繁体   English   中英

诊断WinForms数据绑定问题

[英]Diagnosing WinForms Databinding problem

我正在从这里http://www.akadia.com/services/dotnet_databinding.html学习数据绑定

我只是更新数据集,然后使用sqlDataAdaper.Update()更新sql数据库。

    this.selectCommand = new SqlCommand();
    this.selectCommand.CommandType = CommandType.Text;
    this.selectCommand.Connection = this.connection;

    this.dataAdapter = new SqlDataAdapter(this.selectCommand);
    this.dataAdapter.MissingSchemaAction = System.Data.MissingSchemaAction.AddWithKey;

    this.commandBuilder = new SqlCommandBuilder(this.dataAdapter);
    this.selectCommand.CommandText = "Select `FirstName`,`lastname` from customers where customerId=123";
    this.dataAdapter.Fill(this.dataSet, "customers");

现在绑定为:

// Simple Data Binding
txtBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet, "Customers.FirstName", true));

它显示名字,当我更改名字并点击保存按钮时,将运行以下代码:

    this.dataAdapter.Update(this.dataSet, "customers");

更改后的值不会反映在数据集中,因此也不会反映在数据库中。 我不明白为什么?

BindingContext何时将在Textbox(GUI)中更改的值推送到数据集中? 我有什么想念的吗?

在单击保存按钮时,请执行以下操作:

   CurrencyManager  cm = (CurrencyManager)this.BindingContext[dataSet, "customers"];
   cm.EndCurrentEdit();
   this.dataAdapter.Update(this.dataSet, "customers");

暂无
暂无

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

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