When should the data load in the datagridview? And how are both the forms related? That holds the key to the answer.
1. If form1 opens form2, simplest way could be to either pass the dataset/datatable as constructor parameter to form2. This can be used as the datasource for the gridview.
2. Form2 can also expose a property for the datagrdiview datasource which can be set by form1.
3. If there is any event in form2 on which this happens, preferably, form2 must make it a public event which should be handled by the form1.
Update: I can think of two ways of doing this. Form1 has a DataSet variable which is sent to constructor of form2. Now, in form2, after save is done, set the new dataset to this variable. Then you can use it in the form1. Something like this:
DataSet dataSet = new DataSet();
Form2 form2 = new Form2(dataSet);
In form2:
class Form2{
DataSet _dataSet = new DataSet();
Form2(DataSet dataSet){
_dataSet = dataSet;
}
}
After save is done:
_dataSet = newDataSet;
So now, the variable dataSet that was passed from Form1 should have values from Form2.
Here is another way of doing this:
Assuming Form1 opens Form2 and say you have a Save button to save the data in the database. I will do it this way:
In the Form2:
class Form2{
public event EventHandler SaveButtonClick;
protected virtual void OnSaveButtonClick(EventArgs e){
if(SaveButtonClick != null){
SaveButtonClick(btnSave, e);
}
private void ClickSaveButton(object sender, EventArgs e){
OnSaveButtonClick(e);
}
}
}
In the Form1, this code should follow the lines where you are creating and displaying the Form2:
Form2 form2 = new Form2();
form2.SaveButtonClick += new EventHandler(Form2SaveClick);
Now you should have the method Form2SaveClick in the form1 which will be fired when the Save button is clicked on the Form2. This is how execution flow will happen:
Click Save on Form2 -> ClickSaveButton -> OnSaveButtonClick -> Form2SaveClick
Example I have given has a EventArgs as argument. If you change it to your custom type, you can share the dataset using that. ie the argument "e" in the Form2SaveClick should have a property called DataSet which will come from form2. So all the save will be done before calling OnSaveButtonClick in the Form2. And then, "e.DataSet" of OnSaveButtonClick should be assigned the new dataset.
NOTE: I have written all the code here itself so I am not sure whether this work work or even compile. Someone with VS access can correct things if it's wrong.