简体   繁体   English

无法在 Windows 窗体应用程序 C# 中更新 datagridview 数据

[英]Not able to update datagridview data in windows form application C#

I have two forms in my windows form application.我的 Windows 窗体应用程序中有两个窗体。 I have a datagridview in form1, I am able to update the data of the datagridview from the form1.我在form1中有一个datagridview,我可以从form1更新datagridview的数据。 Now, i need to update the datagridview from form2.现在,我需要从 form2 更新 datagridview。 I am calling the same method from form2 which i am using to update the datagridview from form1.我正在从 form2 调用相同的方法,我用它来更新 form1 中的 datagridview。

While calling the method from form2, in debugging i can see that it updating the datasource of the datagridview, but it not updating the data on UI.从form2调用该方法时,在调试中我可以看到它更新了datagridview的数据源,但没有更新UI上的数据。

I know it is a repeat question.我知道这是一个重复的问题。 I have tried many different ways.我尝试了很多不同的方法。 I have also tried the concept of MdiContainer, but not useful for me.我也尝试过 MdiContainer 的概念,但对我没有用。

I have below methods in form1 which doing job to update data of datagridview(CustomerList) and it updating the datagridview on UI also while calling BindCustomerList() method from form1我在form1中有以下方法来更新datagridview(CustomerList)的数据,它也在从form1调用BindCustomerList()方法时更新UI上的datagridview

//form1
public void BindCustomerList()
{
     CustomerList.AutoGenerateColumns = false;
     CustomerList.DataSource = FetchEmpDetails();
     CustomerList.ClearSelection();
}

public DataTable FetchEmpDetails()
{
      if (sqlCon.State == ConnectionState.Closed)
      {
            sqlCon.Open();
      }
      DataTable dtData = new DataTable();
      sqlCmd = new SqlCommand("sp_customers", sqlCon);
      sqlCmd.CommandType = CommandType.StoredProcedure;
      sqlCmd.Parameters.AddWithValue("@ActionType", "FetchData");
      SqlDataAdapter sqlSda = new SqlDataAdapter(sqlCmd);
      sqlSda.Fill(dtData);
      return dtData;
}

I am calling BindCustomerList() from form2 using below code, which updating the datasource of the datagridview(CustomerList) but not updating on UI in form1.我正在使用以下代码从 form2 调用 BindCustomerList(),该代码更新 datagridview(CustomerList) 的数据源,但不更新 form1 中的 UI。

//form2 button click event    
private void btnSave_Click(object sender, EventArgs e)
{
    Form1 form1 = new Form1();    //object of form1 in form2
    this.Close();                 //closing form2 on button click
    form1.BindCustomerList();     //calling form1 method to update datagridview.
}

Make your form2 constructor accept a datatable parameter, and assign it to the DataSource of the other grid:让您的 form2 构造函数接受一个数据表参数,并将其分配给另一个网格的数据源:

public Form2(DataTable dt){
  InitializeComponent();

  otherDataGridView.DataSource = dt;
}

And when you make a new Form2, in form1, after you already called BindCustomerList at least once:当你在 form1 中创建一个新的 Form2 时,你已经至少调用了一次 BindCustomerList:

var f = new Form2(CustomerList.DataSource as DataTable);

I am calling BindCustomerList() from form2我正在从 form2 调用 BindCustomerList()

It won't work;它不会工作; bindCustomerList sets the DataSource of form1's datagridview; bindCustomerList 设置form1 的datagridview 的DataSource; this is nothing to do with form2.这与form2无关。 To have the forms share data you have to pass the data you want to share, such as in my code above (for example)要让表单共享数据,您必须传递要共享的数据,例如在我上面的代码中(例如)

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

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