诊断WinForms数据绑定问题

时间:2009-11-25 14:25:24

标签: c# .net sql-server ado.net

我正在从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)中更改的值推送到数据集中? 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

单击“保存”按钮,执行以下操作:

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