簡體   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