Calling Method to the other form.

Jamie Hayashi 121 Reputation points
2022-05-04T12:44:51.093+00:00

Good Day, I have a problem calling a method in other form. I have a code and the visual studio don't detect any error but my code did not run, I don't know why. Here's my code.

Form 1

private void btnenter_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.UpdateGridView();
}


Form 2

public void UpdateGridView()
{
//CODE HERE...

}


anybody can told what, where's the wrong of the code??

Microsoft Authenticator
Microsoft Authenticator
A Microsoft app for iOS and Android devices that enables authentication with two-factor verification, phone sign-in, and code generation.
5,543 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,665 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,286 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-05-05T01:49:17.147+00:00

    @Jamie Hayashi , Welcome to Microsoft Q&A, you could try to use Application.OpenForms Property to get what you wanted.

    Here is a code example you could refer to.

    Form1:

         public partial class Form1 : Form  
            {  
                public Form1()  
                {  
                    InitializeComponent();  
                      
                }  
          
                private void button1_Click(object sender, EventArgs e)  
                {  
                    Form2 form = (Form2)Application.OpenForms["Form2"];  
                    form.UpdateGridView();  
                }  
          
                private void Form1_Load(object sender, EventArgs e)  
                {  
                    Form2 form2 = new Form2();  
                    form2.Show();  
                }  
            }  
           
    Form2:  
      
      public partial class Form2 : Form  
        {  
            public Form2()  
            {  
                InitializeComponent();  
            }  
            public void UpdateGridView()  
            {  
                dataGridView1.Rows[1].Cells[1].Value = 23;  
            }  
      
            private void Form2_Load(object sender, EventArgs e)  
            {  
                DataTable dt = new DataTable();  
                dt.Columns.Add("Name");  
                dt.Columns.Add("Age");  
                dt.Rows.Add("test1", 22);  
                dt.Rows.Add("test2", 32);  
                dt.Rows.Add("test3", 12);  
                dataGridView1.DataSource= dt;  
      
            }  
        }  
    

    Result:

    199052-2.gif

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful