2011-03-08 41 views
0

我有一個winform,其中包含一個bindingsource,其數據源是一個類型化的數據集。我在設計器中綁定了兩個文本框到同一列。當我更新任一文本框時,DatSet中的DataRow列將被正確更新,但表單上的其他文本框值不會更新。數據綁定用戶界面中的多個字段不更新

我缺少什麼?如何獲取數據綁定來更新第二個文本框?

注意:這是我需要這樣做的一個簡化示例,因爲在實際應用程序中,因爲一個控件是用戶可編輯的,另一個控件是用於計算的複合控件的輸入。

// Taken from InitializeComponent() 
this.productsBindingSource.DataMember = "Products";  
this.productsBindingSource.DataSource = this.dataSet1; 
this.textBox1.DataBindings.Add(new Binding("Text", this.productsBindingSource, "UnitsInStock", true, DataSourceUpdateMode.OnPropertyChanged)); 
this.textBox2.DataBindings.Add(new Binding("Text", this.productsBindingSource, "UnitsInStock", true, DataSourceUpdateMode.OnPropertyChanged)); 

// Taken from Form Load Event   
DataSet1TableAdapters.ProductsTableAdapter adapter = new DataSet1TableAdapters.ProductsTableAdapter(); 
adapter.Fill(dataSet1.Products); 

回答

0

因此,我的解決方案是在Typed Data Row類上實現INotifyPropertyChanged,因爲它由設計者公開爲分部類,這非常簡單,然後將bindingsource的數據源設置爲數據行本身。將數據綁定

MyDataRow row = <Get Current Row>; 
row.AddEventHandler(); 
bindingSource1.DataSource = row; 
1

有MSDN上這可能有助於一篇文章 - 看How to: Ensure Multiple Controls Bound to the Same Data Source Remain Synchronized

基本上你需要設置一個事件處理程序的BindingSource的BindingComplete事件(如你所做的一切,你需要有FormattingEnabled設置爲True這個工作)

然後,在BindingComplete事件處理程序,你有這樣的代碼:

private void productsBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e) 
     { 
      // Check if the data source has been updated, 
      // and that no error has occured. 
      if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null) 
      { 
       // End the current edit. 
       e.Binding.BindingManagerBase.EndCurrentEdit(); 
      } 
     } 
+0

public partial class MyDataRow : INotifyPropertyChanged { public void AddEventHandler() { this.Table.ColumnChanged += new System.Data.DataColumnChangeEventHandler(Table_ColumnChanged); } void Table_ColumnChanged(object sender, System.Data.DataColumnChangeEventArgs e) { OnPropertyChanged(e.Column.ColumnName); } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion } 

代碼不幸的是,這並不適合我作爲寫入不同的現場發回導致循環中的數據源複合控件重新計算工作。此外,每次編輯單個屬性時,都會犯下一些錯誤。 –

相關問題