【问题标题】:databinding multiple fields in UI not updatingUI中的数据绑定多个字段未更新
【发布时间】:2011-03-08 12:29:23
【问题描述】:

我有一个包含 bindingsource 的 winform,它的数据源是一个类型化的数据集。我在设计器中将两个文本框绑定到同一列。当我更新任一文本框时,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);

【问题讨论】:

    标签: winforms data-binding dataset


    【解决方案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();
                }
            }
    

    【讨论】:

    • 不幸的是,这对我不起作用,因为复合控件重新计算将不同的字段写回数据源导致循环。此外,每次编辑单个属性时都提交整行感觉有点像 hack。
    【解决方案2】:

    所以我的解决方案是在 Typed Data Row 类上实现 INotifyPropertyChanged,因为它被设计器作为部分类公开,这相当简单,然后将 bindingsource 的数据源设置为数据行本身。

    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
    }
    

    数据绑定代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      相关资源
      最近更新 更多