Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have 2 forms : Form1 and Form2
Form1 start running with Application.Run()

and Form1 have a datagridview if user Start Edit the DataGridView
i run Form2 like this

Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();

After Form2 finish the task, When i click a button on Form 2 i want to get value return to Form1 and show in the DataGridView on Form1 and close the Form2

i tried to pass the Form1's object
Form2 addStock = new Form2(productNo, stockString, this);

but on Form2 i can't access the Form1's DataGridview with that object

how can i do to accomplish this?
Posted

Since the Form2 instance is created from Form1, the easiest way is to create a property in Form2 which retrieves the data.
You then either display the form with
1) ShowDialog - the form displays, and when it closes from the OK button or otherwise your Form1 code will continue from that point and can fetch the data:
C#
Form2 f2 = new Form2()
if (f2.ShowDialog == DialogResult.OK)
   {
   string myData = f2.Data;
   ...
   }

2) Show - the form displays, but you need to add a handler to the FormClosed event and retrieve the data there:
C#
Form2 f2 = new From2();
f2.FormClosed += new FormClosedEventHandler(ClosedForm);
f2.Show();
...
private void Closed(object sender, FormClosedEventArgs e)
   {
   Form2 f2 = sender as Form2;
   if (f2 != null)
      {
      string myData = f2.Data;
      ...
      }
   }
 
Share this answer
 
in form1 set modifires for gridview = public

means DataGridview>properties>modifires=public
 
Share this answer
 
Comments
OriginalGriff 10-Nov-13 3:03am    
Reason for my vote of one: This is a very, very poor idea. There are private by default for a reason! If you expose controls to the outside world, you lock the design of your form - you cannot make changes without considering the effects on form that you do not / should not even know exist! Please brush up on your OOPs principles before answering any further questions.

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