Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I need your help.

Do you have any idea on how to display your data into other form that return by the Dataset? I used datagridview in my second form, and I want to display the data that I input in the first form. I'm using c# in this application with the Database MySQL server.

Thanks,
Nico
Posted

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);
}

//Suppose this is the click event handler for the save button in form2
private void ClickSaveButton(object sender, EventArgs e){
// Do some work if needed
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.
 
Share this answer
 
v2
Comments
simplenick 7-Apr-11 0:40am    
This is how my form it worked.

In Form1, I used Datagridview in which all data should be displayed.
In Form2, is my fill-up form and save it to database when it is submitted.

After I submit the data to database I need to return Dataset to my Form2 so that it will display in the Datagridview.

Do you have any idea about this? can you show me a sample code?

Thank you very much
Nico
dan!sh 7-Apr-11 2:55am    
Updated the reply.
Mahendra.p25 7-Apr-11 0:46am    
you want all your records from the database or recently entered record if you want to display the recent record you have entered try following things
amitkarnik2211 7-Apr-11 3:13am    
my 5 this worked
simplenick 7-Apr-11 4:04am    
Thank you very much it really help a lot. I have now the idea on how base on your code. I apply it and it works fine.

From:
Nico
You need to use events to trigger an event when you do something in your Form1 triger that event.
And in your Form2 subscribe to that event, and when the action is trigered you will get that DataSet and use it in that Form2.
I hope this gives you an idea.

http://stackoverflow.com/questions/3842505/call-event-from-form2-in-form1[^]
 
Share this answer
 
Hi,

In form 2 when you successfully submitted your fill up form details into the Database return scopeIdentity which will return you the last primary key inserted value then redirect it from Form 2 to form 1 and pass that primary key value in querystring then in the page load of form 1 check Querystring is not null then on the basis of that querystring value get the data from the database in the dataset and bind it to your GridView
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900