I have some difficulties with understanding BindingSource’s behaviour.
Let’s look at following example:
-
Creating table
DataTable dt = new DataTable();dt.Columns.Add("id", typeof(int));dt.Rows.Add(new object[] { 0 });dt.Rows.Add(new object[] { 1 });dt.Rows.Add(new object[] { 2 });dt.Rows.Add(new object[] { 3 }); -
Creating two BindingSource objects with same DataSource property
BindingSource bs1 = new BindingSource();BindingSource bs2 = new BindingSource();bs1.DataSource = dt;bs2.DataSource = dt;
At this point I supposed, that created BindingSource are fully independent. But really it is not so. After changing Filter property of bs1:
`bs1.Filter = "id >= 2";`
Filter property of bs2 doesn’t change, but RowFilter property of underlying DataView (List property of BindingSource) of both BindingSource objects is changed.
It turns out that both BindingSource objects have exactly same instance of DataView i.e. condition bs1.List == bs2.List is true.
My question is why they share same List and how one can change this behaviour?
EDIT:
I’ve found explanation for “why they’re sharing same List?” – it seems that List is assigned from DataTable‘s DefaultView property (so both bs1.List == bs2.List, bs1.List == dt.DefaultView are true).
It seems that to change this behaviour one can create two different
DataViewinstances forDataTableand assign them toDataSourceproperty of twoBindingSourceobjects accordingly: