简体   繁体   English

将数据以C#的另一种形式添加到datagridview?

[英]Adding Data to a datagridview in another form in C#?

Ok, i have 2 forms. 好的,我有两种形式。 form8 contains the datagrid view. form8包含datagrid视图。 the second(form10) form contains a textbox, and a picturebox. 第二个(form10)表单包含一个文本框和一个图片框。 This is the code i am trying to use to pass the data, but it will not work. 这是我试图用来传递数据的代码,但是它不起作用。

    Form8 frm8;
    public Form10(Form8 frm8): this()
    {
        this.frm8 = frm8;
    }

    private void buttonX1_Click(object sender, EventArgs e)
    {
        try
        {
            int n = frm8.dataGridView1.Rows.Add();
            frm8.dataGridView1.Rows[n].Cells[0].Value = textBox1.Text;
            frm8.dataGridView1.Rows[n].Cells[1].Value = comboBox1.Text + "|" + textBox3.Text;
            frm8.dataGridView1.Rows[n].Cells[2].Value = pictureBox1.Image;
            this.Close();
        }
        catch { }
    }

Please try to refresh the form to get the result. 请尝试刷新表格以获得结果。

frm8.dataGridView1.Invalidate();

or try Application.DoEvents() to get UI thread to refresh. 或尝试使用Application.DoEvents()获取要刷新的UI线程。

Its better that you create an event coming from Form10, and in form8 you register that event so when you click on the button your data is passed to form8. 最好创建一个来自Form10的事件,然后在Form8中注册该事件,这样,当您单击按钮时,数据将传递到Form8。

You should also do something with your exception log it, and also stick to naming convention to make your code more readable 您还应该对异常进行记录,并遵循命名约定以使代码更具可读性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM