Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I pass data from child of child form to parent form in C#?

How do I pass data from child of child form to parent form in C#?

Scheduled Pinned Locked Moved C#
questioncsharpwinformshelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    naouf10
    wrote on last edited by
    #1

    I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you

    Form1:

    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();
    
    }
    
    private void buttonOpenForm2 \_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Show();
    }
    

    }

    Form2:

    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }

    private void buttonOpenForm3 \_Click(object sender, EventArgs e)
    {
        Form3 frm3 = new Form3();
        frm3.Show();
    }
    

    }

    Form3:

    public partial class Form3 : Form
    {
    public Form3()
    {
    InitializeComponent();
    }

    private void buttonAddRow \_Click(object sender, EventArgs e)
    {
        //What to write here to insert the 3 textboxes values into DataGridView?
    
    }
    

    }

    OriginalGriffO B S 3 Replies Last reply
    0
    • N naouf10

      I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you

      Form1:

      public partial class Form1 : Form
      {

      public Form1()
      {
          InitializeComponent();
      
      }
      
      private void buttonOpenForm2 \_Click(object sender, EventArgs e)
      {
          Form2 frm2 = new Form2();
          frm2.Show();
      }
      

      }

      Form2:

      public partial class Form2 : Form
      {
      public Form2()
      {
      InitializeComponent();
      }

      private void buttonOpenForm3 \_Click(object sender, EventArgs e)
      {
          Form3 frm3 = new Form3();
          frm3.Show();
      }
      

      }

      Form3:

      public partial class Form3 : Form
      {
      public Form3()
      {
      InitializeComponent();
      }

      private void buttonAddRow \_Click(object sender, EventArgs e)
      {
          //What to write here to insert the 3 textboxes values into DataGridView?
      
      }
      

      }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Use events and properties: Form3 signals to it's "parent" Form2, which gathers the data and signals an event to it's parent, Form1. Sound complex, but it's really simple! See here: Transferring information between two forms, Part 2: Child to Parent[^]

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      N 1 Reply Last reply
      0
      • N naouf10

        I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you

        Form1:

        public partial class Form1 : Form
        {

        public Form1()
        {
            InitializeComponent();
        
        }
        
        private void buttonOpenForm2 \_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
        }
        

        }

        Form2:

        public partial class Form2 : Form
        {
        public Form2()
        {
        InitializeComponent();
        }

        private void buttonOpenForm3 \_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3();
            frm3.Show();
        }
        

        }

        Form3:

        public partial class Form3 : Form
        {
        public Form3()
        {
        InitializeComponent();
        }

        private void buttonAddRow \_Click(object sender, EventArgs e)
        {
            //What to write here to insert the 3 textboxes values into DataGridView?
        
        }
        

        }

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        What was wrong with the answer you got on November 6th. when you asked the same question: [^] ? Why use three Forms when you can use one Form and UserControls ?

        «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

        N 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Use events and properties: Form3 signals to it's "parent" Form2, which gathers the data and signals an event to it's parent, Form1. Sound complex, but it's really simple! See here: Transferring information between two forms, Part 2: Child to Parent[^]

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          N Offline
          N Offline
          naouf10
          wrote on last edited by
          #4

          Hi, thank you for the help, I will have to study the properties and events then i will test the code written in the answer. thank you

          OriginalGriffO 1 Reply Last reply
          0
          • B BillWoodruff

            What was wrong with the answer you got on November 6th. when you asked the same question: [^] ? Why use three Forms when you can use one Form and UserControls ?

            «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

            N Offline
            N Offline
            naouf10
            wrote on last edited by
            #5

            Hi . nothing wrong but i am new to c# and i have to study the events and property then i will test the code. yes i will try to use form and user control instead, thank you

            1 Reply Last reply
            0
            • N naouf10

              Hi, thank you for the help, I will have to study the properties and events then i will test the code written in the answer. thank you

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You're welcome!

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • N naouf10

                I am new to c#. I have the following in my project in windows forms: Form1 with button and DataGridView. Form2 with button. Form3 with button and 3 textBoxes. In form1, I click buttonOpenForm2 form2 pops up. Then in form2 I click buttonOpenForm3 form3 pops up which has 3 text boxes and button. Now the 3 forms are open. now in form3, I enter values in textBox1, textBox2 and textBox3 and when click buttonAddRow ( from form3) I want these values to be inserted into the DataGRidView in Form1. My question is: How can I add a row into DataGridView in Form1 ( parent) from form3 (child of child form) WITHOUT closing form2 and form3? I mean I want to pass the data while form2 and form3 are still open. Please help me. Thank you

                Form1:

                public partial class Form1 : Form
                {

                public Form1()
                {
                    InitializeComponent();
                
                }
                
                private void buttonOpenForm2 \_Click(object sender, EventArgs e)
                {
                    Form2 frm2 = new Form2();
                    frm2.Show();
                }
                

                }

                Form2:

                public partial class Form2 : Form
                {
                public Form2()
                {
                InitializeComponent();
                }

                private void buttonOpenForm3 \_Click(object sender, EventArgs e)
                {
                    Form3 frm3 = new Form3();
                    frm3.Show();
                }
                

                }

                Form3:

                public partial class Form3 : Form
                {
                public Form3()
                {
                InitializeComponent();
                }

                private void buttonAddRow \_Click(object sender, EventArgs e)
                {
                    //What to write here to insert the 3 textboxes values into DataGridView?
                
                }
                

                }

                S Offline
                S Offline
                susad
                wrote on last edited by
                #7

                Build some business logic, i.e. class(es) for storing the text entered form3 and pass it/them to form1 in buttonAddRow_Click or make something like this

                public struct EnteredText
                {
                public string t1, t2, t3;
                };

                public partial class Form3 : Form
                {
                public delegate void ButtonClick(EnteredText data);
                public event ButtonClick OnClick;
                ...
                private void buttonAddRow _Click(object sender, EventArgs e)
                {
                EnteredText args;
                args.t1 = textbox1.Text; args.t2 = ...
                if (OnClick != null)
                OnClick(args);
                }
                }

                Means you copy the text from the boxes into the struct and fire the event OnClick. Somewhere in Form2 you should do something like this:

                Form3 f3 = new Form3();
                f3.OnClick += f3_OnClick;
                f3.Show();
                }
                private void f3_OnClick(EnteredText txt)
                {
                // do something with the text or refire a new event.
                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups