如何让 BindingSource 知道其 DataSource 的变化?

2024-02-14

我有一个:

someBindingSource.DataSource = someDataSource;

我也这样做:

someDataSource = foo();

foo() does new对于具有不同数据的另一个数据源。

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

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

那么有没有办法让someBindingSource意识到变化someDataSource?


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

另外,如果数据源的项目实现INotifyPropertyChanged https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx,那么BindingSource还将收到有关项目变更的通知。

在上述情况下ListChanged将引发事件。

Note

  1. 注意,如果你分配someBindingSource.DataSource = someThing;进而someThing = new SomeThing();, since someBindingSource.DataSource指向前一个对象,没有变化,也不会有通知。
  2. DataSourceChanged在您分配新值后将引发事件DataSource of a BindingSource,所以之后someThing = new SomeThing();在之前的情况下如果你执行someBindingSource.DataSource = someThing;那么DataSourceChanged将被提高。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让 BindingSource 知道其 DataSource 的变化? 的相关文章

随机推荐