简体   繁体   English

从Form2更新datagridview时更新Form1中的文本框

[英]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中,我将有一些文本框和一个datagridview,当用户单击datagridview form2的编辑时,将弹出该对话框,如果用户进行了一些更改,我将更新更改并重新绑定datagridview这是我的代码

// Form1 Code // Form1代码

    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 Form2按钮单击代码

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 一切正常,除了updateTextBoxes方法没有按要求更新文本框,这可以帮助我

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. 您正在创建一个新的Form1实例,该实例与原始实例无关(可能显示的数据也不相同),因此您的更新代码将作用于此表单实例(顺便隐藏),并且不会更改表单中的任何内容。原始实例。

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 解决此问题的最简单方法是将Form1的实例传递给Form2的构造函数,将该实例保存在Form2中,然后使用此保存的实例调用更新

In form1.cs 在form1.cs中

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

In form2.cs 在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 . 解决此InterForm通讯问题的更好方法是创建和使用自定义事件 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. 在这种情况下,Form2完成更改后,将尝试通知每个对其更改感兴趣的客户端他们需要更新其数据。 The Form1 subscribes the event raised from the Form2 and when notified updates its data.... Form1订阅从Form2引发的事件,并在收到通知时更新其数据。
This approach is considered better because there is less coupling between the two forms. 这种方法被认为更好,因为两种形式之间的耦合较少。

In form2.cs 在form2.cs中

public delegate void OnUpdateData(); 公共委托void OnUpdateData(); public event OnUpdateData UpdateData; 公共事件OnUpdateData UpdateData;

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

In form1.cs 在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 而是创建对调用表单的引用,然后在调用表单中调用UpdateData()方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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