Monday, April 18, 2011

Refresh the Parent Form on Closing the Child Form in C# Windows Application.

For Example :

Parent Form Contains Grid that displays all Employee data and button Called "Add New Employee". On Add button Click, a Child Page is opening, where we can add a new Employee details, and on save button click, data should be saved in database and child form is closed. At the same time the saved employee data should reflect in the datagrid which is there in parent page.

For these kind of Criteria, please see the code below.
     
On Parent Form :

 private void btnAdd_Click(object sender, EventArgs e)
{
   frmChildForm frm2 = new frmChildForm();
   frm2.FormClosed + = new FormClosedEventHandler(frm2_FormClosed);
   frm2.Show();
 }

 private void frm2_FormClosed(object sender, FormClosedEventArgs e)
 {
   DataTable dt = new DataTable();
   //get Eployee data from db and fill it dt;
   dgvTesting.DataSource =  dt;
   this.Close();
 }

16 comments: