Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh datagridview when closing child form?

Tags:

I've a dgv on my main form, there is a button that opens up another form to insert some data into the datasource bounded to the dgv. I want when child form closes the dgv auto refresh. I tried to add this in child form closing event, but it doesn't refresh:

private void frmNew_FormClosing(object sender, FormClosingEventArgs e)         {             frmMain frmm = new frmMain();              frmm.itemCategoryBindingSource.EndEdit();             frmm.itemsTableAdapter.Fill(myDatabaseDataSet.Items);             frmm.dataGridView1.Refresh();         } 

However, when I add this code in a button on the parent form, it actually does the trick:

        this.itemCategoryBindingSource.EndEdit();         this.itemsTableAdapter.Fill(myDatabaseDataSet.Items);         this.dataGridView1.Refresh(); 
like image 470
DanSogaard Avatar asked Mar 07 '10 08:03

DanSogaard


2 Answers

There are many ways to do this, but the following is the simpliest and it will do what you want and get you started.

  • Create a public method on your main form.
  • Modified constructor of second form to take a main form.
  • Create an instance of the second form passing the main form object.
  • When closing second form, call the public method of the main form object.

Form1

public partial class Form1 : Form {     public Form1() {         //'add a label and a buttom to form         InitializeComponent();     }     private void button1_Click(object sender, EventArgs e) {         Form2 oForm = new Form2(this);         oForm.Show();     }     public void PerformRefresh() {         this.label1.Text = DateTime.Now.ToLongTimeString();     } } 

Form2

public class Form2 : Form {     Form1 _owner;     public Form2(Form1 owner) {         _owner = owner;         this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);     }     private void Form2_FormClosing(object sender, FormClosingEventArgs e) {         _owner.PerformRefresh();     } } 
like image 96
AMissico Avatar answered Oct 21 '22 03:10

AMissico


We can also proceed this way:

We have form_1 and form_2

  1. In form_1, create a public method. Inside this public method we put our stuff;
  2. In form_2 we create a global form variable;
  3. Still in form_2, pass form_1 into form_2 through form_2 constructor;
  4. Still in form_2, make your global variable(the one we created in step 2) receive the new form_1 instance we created in form_2 constructor;
  5. Inside the closing_event method we call the method which contains our stuff.

The method with our stuff is the method that will fill our form1 list, dataGridView, comboBox or whatever we want.

Form_1:

public fillComboBox()//Step 1. This is the method with your stuff in Form1 {      foreach(var item in collection myInfo)      {myComboBox.Items.Add(item)}  } 

Form_2:

Form1 instanceForm1;//Step 2 public Form2(Form1 theTransporter)//Step 3. This the Form2 contructor.  {      InitializeComponent();     instanceForm1 = theTransporter;//Step 4 }  private void Form2_FormClosing(object sender, FormClosingEventArgs e) {     instanceForm1.fillComboBox();//Step 5 we call the method to execute the task updating the form1 } 

I hope it helps...

like image 39
Marques Patricio Mamba Avatar answered Oct 21 '22 04:10

Marques Patricio Mamba