如何使BindingSource知道其DataSource中的更改? [英] How to make a BindingSource aware of changes in its DataSource?

查看:327
本文介绍了如何使BindingSource知道其DataSource中的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个:

someBindingSource.DataSource = someDataSource;

我也做:

someDataSource = foo();

foo() new 另一个不同数据源的数据源。

foo() does new for another data source with different data.

我不认为每次数据源更改时都进行分配是正确的,即:

I don't think it's correct to do the assignment every time the data source changes, i.e:

someDataSource = foo();
someBindingSource.DataSource = someDataSource;

所以有一种方法使 someBindingSource 意识到 someDataSource 中的变化?

so is there a way to make someBindingSource aware of change in someDataSource?

推荐答案

如果数据源实现< a href =https://msdn.microsoft.com/en-us/library/system.componentmodel.ibindinglist(v=vs.110).aspx =nofollow> IBindingList ,然后将通知 BindingSource 将数据源添加或删除。一个很好的实现方法是 BindingList< T>

If the data source implements IBindingList inteface, then the BindingSource will be informed of adding or removing items to the data source. A good implementation to use is BindingList<T>.

如果数据源的项目实现 INotifyPropertyChanged ,那么 BindingSource 也将被通知项目的更改。

Also if the items of the data source implement INotifyPropertyChanged, then the BindingSource also will be notified of changes on items.

在上述情况下 ListChanged 事件将被提升。

In above cases ListChanged event will be raised.

注意


  1. 注意,如果您分配了 someBindingSource.DataSource = someThing; 然后 someThing = new SomeThing(); ,因为 someBindingSource.DataSource 指向上一个对象,没有任何变化,没有任何通知。在将新值分配给 DataSource 之后,会引发事件数据源
  2. 一个 BindingSource ,所以在 someThing = new SomeThing(); 之前,如果执行 someBindingSource.DataSource = someThing; 然后 DataSourceChanged 将被提升。

  1. Pay attention, If you assign someBindingSource.DataSource = someThing; and then someThing = new SomeThing();, since someBindingSource.DataSource is pointing to previous object, there is no change and there would be no notifications.
  2. DataSourceChanged event will be raised after you assign a new value to DataSource of a BindingSource, so after someThing = new SomeThing(); in previous situation if you perform someBindingSource.DataSource = someThing; then the DataSourceChanged will be raised.

这篇关于如何使BindingSource知道其DataSource中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆