简体   繁体   中英

Update textboxes in Form1 on updating the datagridview from Form2

Hi all I have coded where my functionality as follows, In my form1 I will have some text boxes and a datagridview where when user click on edit of the datagridview form2 will come as popup and if the user make some changes I will update the changes and rebind the datagridview this is my code

// Form1 Code

    public void UpDateData(DataTable dt)
   {
     dataGridView1.Refresh();
     dataGridView1.DataSource = dt;
     updateTextBoxes();
   }
    public void updateTextBoxes()
    {
      //  Some caluculation based on the datagridview rows
    }

Form2 Button click code

private void button1_Click(object sender, EventArgs e)
{
   // Some code to collect the changes made
   Form1 f=new Form1();
   f.UpDateData(dt);
   this.close();
 }

Every thing works fine except the updateTextBoxes method this is not updating the textboxes as per required can some one help me

You are creating a new instance of Form1 that has nothing to do with the original instance (and perhaps has not the same data displayed) so your update code acts on this form instance (hidden by the way) and doesn't change anything in the original instance.

The simplest method to resolve this problem is passing the instance of Form1 to the constructor of Form2, save this instance inside Form2 and call the update using this saved instance

In form1.cs

 Form2 f = new Form2(this);
 f.Show();

In form2.cs

Form1 _callingInstance;
public Form2(Form1 caller)
{
     _callingInstance = caller;
}

private void button1_Click(object sender, EventArgs e)
{
   _callingInstance.UpDateData(dt);
   this.Close();
 }

A slightly better approach to this InterForm Communication problem is through the creation and use of a Custom Event . In this scenario, the Form2, when has completed the changes, tries to notify every client interested to its change that they need to update their data. The Form1 subscribes the event raised from the Form2 and when notified updates its data....
This approach is considered better because there is less coupling between the two forms.

In form2.cs

public delegate void OnUpdateData(); public event OnUpdateData UpdateData;

private void button1_Click(object sender, EventArgs e)
{
   if(UpdateData != null) UpdateData();
   this.Close();
 }

In form1.cs

 Form2 f = new Form2();
 f.UpdateData += myCallbackUpdateMethod;


 public void myCallbackUpdateMethod()
 {
    DataTable dt = GetTable();
    UpDateData(dt)
 }

Use the folowing code , it will solve your problem

Don't create a new instance of your form. Instead create a reference to the calling form and then call the UpdateData() method in calling form

  //Form1 f=new Form1();
   callingform.UpDateData(dt);
   this.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM