locked
C# RRS feed

  • Question

  • Form1 : I create for Display Information In DataGridView 

    Form2: I create for Input Information 

    How to I make Refresh DataGridView In Form1 When I Click Save Button In Form2 On Event Form2 Closed ?  


    • Edited by sokchab Saturday, September 24, 2016 1:24 PM
    • Moved by Kevin Linq Monday, September 26, 2016 3:26 AM
    Saturday, September 24, 2016 1:22 PM

Answers

  • Hi sokchab,

    I understand that you are facing the issue that passing data between two Forms. there are many ways to do this, for example using the constructors to pass data, using a public method to pass data ,using a property to pass data.

    In a word, you want to pass data from form1 to form2, you must get the pointer of  form2 in the form1. And then using the property\ constructor \public method\ static variable\delegate\event to achieve the passing data.

     

    Link below is a walkthrough that using a method to pass data between two Forms.

    https://msdn.microsoft.com/en-us/library/ms171925.aspx

     How to I make Refresh DataGridView In Form1 When I Click Save Button In Form2 On Event Form2 Closed ?  

     Add code like below in form1:

          public void update_dgv(object obj)
            {
                //update the datagridView
            }
            private void button1_Click(object sender, EventArgs e)
            {
                this.Hide();
                Form2 f2 = new Form2();
                f2.f1 = this;
                f2.Show();
            }

    Add code like below in form2:

            private void Form2_FormClosed(object sender, FormClosedEventArgs e)
            {
                object data = null;
                f1.update_dgv(data);
                f1.Show();
            }

    Best Regards,

    Wednesday, September 28, 2016 6:29 AM

All replies

  • How to I make Refresh DataGridView In Form1 When I Click Save Button In Form2 On Event Form2 Closed ?  

    You make a public method in the form1 class that deals with the grid.Refresh(), and you call the method on the form2 close event.

    https://www.dotnetperls.com/public

    You pass the form1 object into the form2's constructor and call the form1's method you created to refresh the grid.

    http://stackoverflow.com/questions/20876760/pass-windows-form-instance-as-parameter

    Saturday, September 24, 2016 2:15 PM
  • Here's another suggestion using an Interface (from my blog):

    http://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, September 24, 2016 4:54 PM
  • Hi sokchab :

    Thank you for posting here.

    Since your issue is related to Win form development, I will move your thread to Windows form general forum . 

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Here is the link of Winform general forum:https://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms

    If you have anything else regarding C# programming , please feel free to let me know . 

    Best Regards,

    Kevin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, September 26, 2016 3:24 AM
  • Hi sokchab,

    I understand that you are facing the issue that passing data between two Forms. there are many ways to do this, for example using the constructors to pass data, using a public method to pass data ,using a property to pass data.

    In a word, you want to pass data from form1 to form2, you must get the pointer of  form2 in the form1. And then using the property\ constructor \public method\ static variable\delegate\event to achieve the passing data.

     

    Link below is a walkthrough that using a method to pass data between two Forms.

    https://msdn.microsoft.com/en-us/library/ms171925.aspx

     How to I make Refresh DataGridView In Form1 When I Click Save Button In Form2 On Event Form2 Closed ?  

     Add code like below in form1:

          public void update_dgv(object obj)
            {
                //update the datagridView
            }
            private void button1_Click(object sender, EventArgs e)
            {
                this.Hide();
                Form2 f2 = new Form2();
                f2.f1 = this;
                f2.Show();
            }

    Add code like below in form2:

            private void Form2_FormClosed(object sender, FormClosedEventArgs e)
            {
                object data = null;
                f1.update_dgv(data);
                f1.Show();
            }

    Best Regards,

    Wednesday, September 28, 2016 6:29 AM