Using multiple BindingSources in a single data table

I have a DataTable with a boolean column named [Invalid]. I need to split this data into this invalid column - valid rows can be edited, invalid rows cannot. My initial plan was to use two BindingSources and set the Filter property ([Invalid] = 'false', for example), which plays directly into my hands, because I have two DataGridViews and therefore I need two BindingSources.

This does not work: BindingSources sets the Filter property associated with the DataTable, so both BindingSources contain the same data. Should I do two selections from the database, or can I do what I want with the objects that I have?

+5
source share
2 answers

I do not think you can do it the way you hope.

Can you use two different data types of the same data type and bind your data to datagridview them?

+5
source

When you bind to a DataTable, you actually bind to its default view (DataTable.DefaultView). And when you set the Filter property as a BindingSource, you set the Filter property for the default view, overwriting the filter set by another BindingSource.

So the deadcat answer is correct : you need to bind to two different views in the DataTable (one of them could be DefaultView, if you prefer):

bindingSource1.DataSource = myDataTable;
bindingSource2.DataSource = new DataView(myDataTable);

or

bindingSource1.DataSource = new DataView(myDataTable);
bindingSource2.DataSource = new DataView(myDataTable);
+5
source

All Articles