How to make a BindingSource aware of changes in its DataSource?
Asked Answered
M

1

2

I have a:

someBindingSource.DataSource = someDataSource;

And I also do:

someDataSource = foo();

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;

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

Marcello answered 18/9, 2016 at 12:0 Comment(3)
You are not making changes in someDataSource but replacing the entire object, So i don't think it'll be posssible without wrapping the someDataSource with a container and changing the data via some container method (take a look at ObservableCollection<T>)Withoutdoors
I'll hold an ObservableCollection with one item?Marcello
I think the point is that you must wrap your dataSource object and replace the data via some method, if you want to replace the data source (instead of updating it) and still have your bindingSource be notifiedWithoutdoors
I
4

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>.

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

In above cases ListChanged event will be raised.

Note

  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.
Ichthyosaur answered 18/9, 2016 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.