none
About passing data between child form to parent form

    Question

  • Hi,

             I do have a form1 here and just do a form2.ShowDialogue();

    so imagine that we are now directed to Form2. i do have datagridview in form2 and i databind the values of the selected row in datagrid to my labels in form2.

    the question is how to pass this value from Form2 to Form1 again.

    i think the problem is, i should need to refresh the form1 just to have these changes.

    Or any other suggestions?

    Is it possible to click a datagridview row in form2 and just automatic pass the value to form1?
    If possible can you please help me? thanks :)

    regards,
    nemesiscsharp
    Monday, January 11, 2010 8:13 AM

Answers

  • Hi Nemesiscsharp,

           Just declare a public variable Eg: var1 in Form2 and on selection of rows from Grid assign the selected value to the Form2 public variable var1.

          then Once you close the Form2. You can access the values in Form1 by say you have a textbox in Form1 which should get the selected value from grid of Form2 by mentioning as

    Form2 f2=new Form2();
    TextBox1.Text=f2.var1;

    Hope this helps
    Monday, January 11, 2010 12:17 PM
  • Hi,

    Just make a PUBLIC variable which shall be global and,
    then make a function in your childform which is public and can
    be reached on the Form1.cs code.

    There you can the say: ChildFrm.TakeData(variable);  <-- Pass it in like this.

    Like this:  Form2.GetValue("");

    And then a static function, like this:

    public static void GetValue(string variable)

    {

    }

    Then another PUBLIC variable in Childform will get the
    value and then you can pass it around or do what you want.

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    Monday, January 11, 2010 8:30 AM
  • in my Form1
    private void button1_Click(object sender, EventArgs e)
    {
    Form2 frm = new Form2(this);
    frm.ShowDialog();
    }

    while in my Form2

            private Form1 parent;

    public Form2(Form1 parent_form)
    {
    parent = parent_form;
    InitializeComponent();
    }

    private void BindButton_Click(object sender, EventArgs e)
    {
    parent.Namelbl.Text = Form2txt.Text;
    }
    • Marked as answer by nemesiscsharp Monday, January 11, 2010 1:29 PM
    Monday, January 11, 2010 1:22 PM
  • Thank you so much for help sir. :)

    Thank you MSDN and all the contributor of this forum, may you continue to help me in other problems.. :)

    God bless

    regards,
    nemesis

    Yes we will, however, please mark my and
    Veeraraghavan Thirumale replaies as well since they
    are correct as well, the above replaies... 

    Coder24.com
    • Marked as answer by nemesiscsharp Tuesday, January 19, 2010 4:44 PM
    Monday, January 11, 2010 1:33 PM

All replies

  • Hi,

    Just make a PUBLIC variable which shall be global and,
    then make a function in your childform which is public and can
    be reached on the Form1.cs code.

    There you can the say: ChildFrm.TakeData(variable);  <-- Pass it in like this.

    Like this:  Form2.GetValue("");

    And then a static function, like this:

    public static void GetValue(string variable)

    {

    }

    Then another PUBLIC variable in Childform will get the
    value and then you can pass it around or do what you want.

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    Monday, January 11, 2010 8:30 AM
  • Can you give me much clearer examples sir? Sorry for being a noob.

    Or give some sample codes? :)
    Thanks for the effort sir
    Monday, January 11, 2010 11:58 AM
  • Hi Nemesiscsharp,

           Just declare a public variable Eg: var1 in Form2 and on selection of rows from Grid assign the selected value to the Form2 public variable var1.

          then Once you close the Form2. You can access the values in Form1 by say you have a textbox in Form1 which should get the selected value from grid of Form2 by mentioning as

    Form2 f2=new Form2();
    TextBox1.Text=f2.var1;

    Hope this helps
    Monday, January 11, 2010 12:17 PM
  • Hi again:

    I do not have any samples for the moment, however,
    it is as we told you two PUBLIC variables, one public function which shall be void
    in Form1 and the the DataGrid.Select.Row(index[]); etc.

    And then Form1.GetValue(temp); <-- Pass the value, and then it will appear in
    the form1.

    Read here:
    http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/47e9c3ef-a8de-48c9-8e0d-4f3fdd34517e

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Monday, January 11, 2010 12:59 PM
  • I do have this code so fat

    in my Form1
    private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                frm.ShowDialog();
            }

    while in my Form2

    public Form2(Form1 parent_form)
            {
                InitializeComponent();
                parent_form.Namelbl.Text = Form2txt.Text;
            }

    My problem is.. in my Form2.. how to set this binding only when i click the "Bind" button in my Form2

    because in my code.. it automatically binds my data whenever i open the form2


    Help.

    regards,
    nemesis
    Monday, January 11, 2010 1:16 PM
  • in my Form1
    private void button1_Click(object sender, EventArgs e)
    {
    Form2 frm = new Form2(this);
    frm.ShowDialog();
    }

    while in my Form2

            private Form1 parent;

    public Form2(Form1 parent_form)
    {
    parent = parent_form;
    InitializeComponent();
    }

    private void BindButton_Click(object sender, EventArgs e)
    {
    parent.Namelbl.Text = Form2txt.Text;
    }
    • Marked as answer by nemesiscsharp Monday, January 11, 2010 1:29 PM
    Monday, January 11, 2010 1:22 PM
  • Thank you so much for help sir. :)

    Thank you MSDN and all the contributor of this forum, may you continue to help me in other problems.. :)

    God bless

    regards,
    nemesis
    Monday, January 11, 2010 1:31 PM
  • Thank you so much for help sir. :)

    Thank you MSDN and all the contributor of this forum, may you continue to help me in other problems.. :)

    God bless

    regards,
    nemesis

    Yes we will, however, please mark my and
    Veeraraghavan Thirumale replaies as well since they
    are correct as well, the above replaies... 

    Coder24.com
    • Marked as answer by nemesiscsharp Tuesday, January 19, 2010 4:44 PM
    Monday, January 11, 2010 1:33 PM
  • Done :) thanks
    Tuesday, January 19, 2010 4:44 PM