TharinduLucky TharinduLucky - 2 months ago 16x
C# Question

Update DataGridView after Child Form Closed

I know lot of similar questions are out there. But, I'm really new to C# so, cannot figure out how to solve this.

I have a

on main form and I have a dialog box to add new record. So, what I want is to reload/refresh the
on main form when the dialog box closes. (Press Save button on dialog box).

So, I have a public method on the main form like this, which I load data using this method:

public void UpdateProductsList()
String query = "SELECT * FROM product";
con = new SqlConnection(conString);
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
dataGridView1.DataSource = dt;

I use this code to open the child form:

private void AddProductButton_Click(object sender, EventArgs e)
Add_product obj = new Add_product();

Now I call this method on the Child form when the Save button is clicked.

private void SaveProductButton_Click(object sender, EventArgs e)
Products products = new Products();

However, this is not working when the Save button is clicked. Strange thing is, when I add this method to a local button on the main form, it works without a problem.

I also added a
and I'm sure it was called, also the data was inserted in database, but the
doesn't show new record.

So, where I've gone wrong ?


When you show the child form using ShowDialog yo don't need to call LoadData from child form, instead you can check the result of ShowDialog and if it was DialogResult.OK, call the method.

Also in your child form, in your save button after saving data, set this.DialogResult = DialogResult.OK.

Show Child Form

using (var f = new ChildForm())
    if(f.ShowDialog()== System.Windows.Forms.DialogResult.OK)
        this.LoadData(); /*Load data in list form*/

Save Button in Child Form

this.SaveData(); /*Save Data in child form */
this.DialogResult = System.Windows.Forms.DialogResult.OK;


  • When ShowDialog is called, the code following it is not executed until after the dialog is closed.
  • If you show a form using ShowDialog, setting DialogResult property of Form closes the form.
  • You can set DialogResult to OK in your save button and set it to Cancel in your cancel button.
  • Currently your problem is in code of your save button, you have created a new instance of your list form and called its UpdateProductsList method. It doesn't have any impact on the open instance of your list form which you can see. It's a different isntace.