Refresh Datagrid of parents Mdichild
Hi Good morning everyone, I have a big problem regarding MdiChild. My problem is that I have 3 forms loaded;
Form1=Mainform / mdiParent
Form2= Display the Datagrids
Form3= This is where I let the user input the necessary fields like Lastname, Firstname,Age,etc...
My problem goes like this, Form3 is the child of Form2, Form2 is the child of Form1 and I call them one by one like this:
Code:
//form1
Form2.MdiParent = this;
Form2.Show();
//form2
Form3.MdiParent = this.MdiParent;
Form3.Show();
//form3
I want in the Form3 to save data and refresh on my datagrid on Form2 so that every data that I encoded in Form3 will reflect in Form2. I let the Form3 close everytime there is data entered but my problem is that the data that entered doesn't reflect in my datagrid. Could anyone help me on this?
Thanks,
Jeff
Re: Refresh Datagrid of parents Mdichild
we'll need to see your code
Re: Refresh Datagrid of parents Mdichild
Hi paul thanks for the reply. What do you want to see paul? Is it the way I load the data I have on my datagrid or the way I call the MdiChild.
or this is it:
Code:
//This is how I load new mdiChild from my MainForm w/c is mdicontainer
Form2 mySecondForm = new Form2();
mySecondForm.MdiParent = thist;
mySecondForm.StartPosition = FormStartPosition.CenterScreen;
mySecondForm.Show();
//This is how I load new mdiChild from my Form2 to Form3
Form3 myThirdForm = new Form3();
myThirdForm.MdiParent = this.MdiParent;
myThirdForm.StartPosition = FormStartPosition.CenterScreen;
myThirdForm.Show();
//This is how I load data in my datagrid
private void DisplayData()
{
String cnString = configurationManager.ConnectionStrings["Payroll"].ConnectionString;
myConnection = new System.Data.SqlClient.SqlConnection(cnString);
myConnection.Open();
myDataSet = new System.Data.DataSet();
myDataSet.CaseSensitive = true;
myCommand = new System.Data.SqlClient.SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select id_no, lastname, firstname, midname, nickname, addr_street from EmpMasterTemp";
// create the DataAdapter object and pass in the
// SQL Command object and establish the table mappings
DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
DataAdapter.SelectCommand = myCommand;
DataAdapter.TableMappings.Add("Table", "EmpMasterTemp");
DataAdapter.Fill(myDataSet);
dataGridView1.DataSource = myDataSet.Tables["EmpMasterTemp"].DefaultView;
myConnection.Close();
}
Re: Refresh Datagrid of parents Mdichild
in form3, do you bind the editing controls to the same datatable as the dgv in form2?
Re: Refresh Datagrid of parents Mdichild
First up, Form3 is NOT a child of Form2. Form3 and Form2 are both children of Form1. Your own code says so:
Code:
Form3.MdiParent = this.MdiParent;
Form3.Show();
That code says "get my parent and make it the parent of Form3", i.e. Form2 and Form3 have the same parent. Form2 and Form3 are siblings.
As for the question, there is only one proper way to do this. Form3 should raise an event and that event can be handled by Form1. Form1 then gets any required information from Form3 by getting properties and/or calling methods. Form1 then calls a method in Form2 to tell it to update or refresh or whatever. Doing that is complicated by the fact that you are actually showing Form3 from Form2 in the first place. There are two ways to go:
1. Change the way you display Form3. Again, the child (Form2) raises and event that is handled by the parent (Form1) and the parent then does the work (displays Form3).
2. Have Form3 raise an event as suggested earlier but have Form2, which created Form3 in the first place, handle the event itself and update or refresh or whatever.
If you don't know how to define and raise your own events then follow the Blog link in my signature and check out my post on the topic.
Re: Refresh Datagrid of parents Mdichild
Actually, now that I read the original post more carefully, I see that you're actually doing things the wrong way around, as so many people do. If you already have data in a grid then you don't save data to the database and then refresh the grid from the database. You make changes to the grid first, then save those changes from the grid (more correctly, the underlying DataTable) back to the database. You update the app first and then the database, not the other way around.
Also, there's very little doubt that Form3 should be a modal dialogue and not an MDI child.
Re: Refresh Datagrid of parents Mdichild
Thanks jim... you're right... Form3 should be a modal dialouge... Actually I tried to restructured my code.. Maybe I need to put all datas to datagrid first before saving all datas that I entered from form3.