How to make BindingSource aware of changes in its DataSource?

I have:

someBindingSource.DataSource = someDataSource; 

And I also do:

 someDataSource = foo(); 

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

I do not think it is right to complete the task every time the data source changes, i.e.

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

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

+2
source share
1 answer

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

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

In the above cases, the ListChanged event will be raised.

Note

  • Please note: if you assign someBindingSource.DataSource = someThing; , and then someThing = new SomeThing(); , since someBindingSource.DataSource points to the previous object, there are no changes, and there will be no notifications. Event
  • DataSourceChanged will be created after you assign a new DataSource BindingSource value, so after someThing = new SomeThing(); in the previous situation, if you do someBindingSource.DataSource = someThing; , then DataSourceChanged will be raised.
+2
source

Source: https://habr.com/ru/post/1257612/


All Articles