Click here to Skip to main content
16,015,218 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I've two forms (Form1 and Form2).
Form1 contains DataGridView and Add button.
While running the form, 1st Form1 will be open, retaining as it is, then Form2 will be opening as I clicked Add button.

my question is After closing Form2,Form1 should get stay as it is and updated itself.

Please reply with an example or sample code.
Posted
Updated 19-Jan-11 21:50pm
v3

I presume you are using Form.Show?
This will treat it as a new form.

Try using Form.ShowDialog() instead.

This will get the Data from the original Form by accessing the previous Form properties.

Hope that helps.
 
Share this answer
 
Comments
JF2015 20-Jan-11 3:53am    
The question is a repost from http://www.codeproject.com/Questions/148221/how-to-update-and-show-existing-Windows-Form.aspx where the OP already got the same answers.
you can send your object of form1 to form2 and add a constructor with a parameter object of form2 like this :

In form1 when you are calling form2 :

Form2 frm2 = new Form2(this);


In form2 you can create a constructor like this :

public static Form1 Frm1 = new Form1();

public Form2(Form1 form)
{
     Frm1 = form
}


now you can access any control of form1 after setting its modifier to public.
 
Share this answer
 
Comments
ks ravi 20-Jan-11 4:51am    
hi michael,
thanks for reply,but its not woking. please reply with an exp. to this that whatever do in other form,control should go back to main form only.
Michael Waguih 20-Jan-11 5:15am    
In the save button in form2 you can update form1 by using the (Frm1) object then you must close it at the end using this.close(); to return to form1 .
I am using it several times in my code and it is working great .
I hope this can help .
ks ravi 20-Jan-11 5:27am    
hi Michael,
second time also not getting;
I must include this:Form2 frm2 = new Form2(this);if yes,where i should include that. please can i have little more detail about your coding.
Hi Ravi ,
first you must create an overloaded constructor in form2 and a global object of form1 , your already existing default constructor is :

public Form2()
{
     InitializeComponent();
}


The global object and the overloaded constructor code will be :

public static Form1 Frm1 = new Form1();

public Form2(Form1 form)
{
     InitializeComponent();
     Frm1 = form;
}


Now you can set your datagridview in form1 's modifier to public .

Then in the click add button event you will insert this code for opening form2 :

Form2 Frm2 =new Form2(this);
Frm2.show();


now in the form2 code you can update your datagridview in form1 using :

Frm1.dataGridView1.Rows[...].Cells[...].Value = "<your desired="" data="">";
</your>


I Hope this time it is clear ,
Good luck
:)
 
Share this answer
 
Comments
ks ravi 20-Jan-11 7:32am    
thank you very much for reply,sorry i did't explain early that i want to refresh or update Mainform from Form2. i used following codes: this.refresh(); this.update(); refreshform()//function,are not working. please reply with an exp.

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