如何使BindingSource知道其DataSource中的更改? [英] How to make a BindingSource aware of changes in its 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.
注意
- 注意,如果您分配了
someBindingSource.DataSource = someThing;
然后someThing = new SomeThing();
,因为someBindingSource.DataSource
指向上一个对象,没有任何变化,没有任何通知。在将新值分配给DataSource
之后,会引发事件数据源$>
-
一个
BindingSource
,所以在someThing = new SomeThing();
之前,如果执行someBindingSource.DataSource = someThing;
然后DataSourceChanged
将被提升。
- Pay attention, If you assign
someBindingSource.DataSource = someThing;
and thensomeThing = new SomeThing();
, sincesomeBindingSource.DataSource
is pointing to previous object, there is no change and there would be no notifications. DataSourceChanged
event will be raised after you assign a new value toDataSource
of aBindingSource
, so aftersomeThing = new SomeThing();
in previous situation if you performsomeBindingSource.DataSource = someThing;
then theDataSourceChanged
will be raised.
这篇关于如何使BindingSource知道其DataSource中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!