Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple BindingSources on one DataTable

Tags:

.net

database

I have a DataTable that has a boolean column called [Invalid]. I need to divide this data up by this Invalid column - valid rows can be edited, invalid rows cannot. My original plan was to use two BindingSources and set the Filter property ([Invalid] = 'false', for instance), which plays right into my hands because I have two DataGridViews and so I need two BindingSources anyway.

This doesn't work: the BindingSources set the Filter property associated with the DataTable, so both BindingSources hold the same data. Am I going to have to do two fetches from the database, or can I do what I want with the objects I have?

like image 754
Merus Avatar asked Oct 21 '08 04:10

Merus


2 Answers

I don't think you can do it the way you are hoping.

You could use two different dataviews of the same datatable, and bind your datagridviews to those?

like image 162
Danny Frencham Avatar answered Sep 28 '22 16:09

Danny Frencham


When you bind to a DataTable, you are effectively binding to its default view (DataTable.DefaultView). And when you set the Filter property of either BindingSource, you are setting the Filter property of the default view, overwriting the Filter set by the other BindingSource.

So deadcat's answer is correct: you need to bind to two different views on the DataTable (one of which can be the DefaultView if you prefer):

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

or

bindingSource1.DataSource = new DataView(myDataTable);
bindingSource2.DataSource = new DataView(myDataTable);
like image 36
Joe Avatar answered Sep 28 '22 14:09

Joe