Hunter Mitchell
Hunter Mitchell

Reputation: 7293

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 { }
    }

Upvotes: 1

Views: 3312

Answers (2)

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

Please try to refresh the form to get the result.

frm8.dataGridView1.Invalidate();

or try Application.DoEvents() to get UI thread to refresh.

Upvotes: 1

JohnnBlade
JohnnBlade

Reputation: 4327

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

Upvotes: 0

Related Questions