Search code examples
c#formsdatagridviewrefresh

refresh a dataGridView from another form


I have Form1 and Form2.

Form1 has a dataGridView and a button for opening Form2. I have written a method in Form1 as below that perfectly refreshes the dataGridView:

public void RefreshGrid()
    {
        dataGridView1.DataSource = empControl.SelectAll(); //Works great
    }

In Form2 I insert into the table and use the below Code for Calling the above method. When I traced the code I saw that it implements all the way but the dataGridView isn't refreshed!

private void btnInsert_Click(object sender, EventArgs e)
    {
            //Insert Code (Inserts perfectly)

            Form1 frm = new Form1();
            frm.RefreshGrid();
            this.Close();
        }
    }

I also tried the FormClosing Event of Form2 but it didn't do the trick.

Help me out plz!


Solution

  • Your problem is that you're creating a new instance of Form1. Instead of new Form1 you need to pass an instance of the existing Form1 to Form2