简体   繁体   中英

Adding Data to a datagridview in another form in C#?

Ok, i have 2 forms. form8 contains the datagrid view. the second(form10) form contains a textbox, and a picturebox. 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.

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.

You should also do something with your exception log it, and also stick to naming convention to make your code more readable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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