【问题标题】:Commit changes to gridview from another form从另一个表单提交对 gridview 的更改
【发布时间】:2013-04-07 07:57:09
【问题描述】:

我有一个 form1,其中包含一个绑定到客户表的 gridview,当用户双击 gridview 行时,会出现一个新的 form2,让用户修改数据。当用户修改数据并单击 form2 中的保存按钮时,它将更改保存到数据库,但更改不会出现在 form1 gridview 中,直到 form1 重新打开。为什么?

//FORM1 when user double click on rows to edit 
private void radGridView1_CommandCellClick(object sender, EventArgs e)
{
    GridCommandCellElement gCommand = (sender as GridCommandCellElement);     
    string v = gCommand.RowInfo.Cells["CustomerID"].Value.ToString();
    Form2 f2 = new Form2(v);
    f2.Show(this);
}

//FORM2 when user click on save button
private void button1_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(WindowsFormsApplication4TELERIK.Properties.Settings.Default.NorthwindConnectionString))
    {
       con.Open();
       using (SqlTransaction tran =con.BeginTransaction(IsolationLevel.Serializable))
       {
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandText = "update customers set CompanyName='" + this.radTextBoxControl1.Text +"'";
          tran.Commit();
          cmd.ExecuteNonQuery();
       }
    }
    this.Close();
}

【问题讨论】:

  • 一旦你更新了字段,再次绑定gridview。
  • 我无法从 form2 访问 gridview !!!@PraveenNambiar
  • 看起来像 Windows 应用程序...对吗?
  • 检查下面发布的答案。

标签: c# visual-studio-2010 gridview


【解决方案1】:

它并不像我们想象的那么简单。

以下链接中提到了一种简单的方法。只需按照其中提到的步骤操作即可。

Refresh GridView from Form 2

【讨论】:

  • 非常感谢,但我还有一个问题:我不想填充表格适配器,因为它丢失了网格中的修改记录!所以让我知道是否有解决方案
猜你喜欢
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
相关资源
最近更新 更多