How to: Different filters for two binding sources with the same data source

N

nvx

Hi,
is it possible to set different filters for two binding sources with
the same data source? If so, how? I tried to use this code:

BindingSource bindingSource1 = new BindingSource();
BindingSource bindingSource2 = new BindingSource();

bindingSource1.DataSource = myDataSet.Tables["tblname"];
dataGridView1.DataSource = bindingSource1;
dataGridView1.Columns["colname1"].DataPropertyName = "colname1db";
// etc. for every column from the DGV1

bindingSource2.DataSource = myDataSet.Tables["tblname"]; // i.e. the
same source as for the first bindingSource1
bindingSource2.Filter = "filtercol = 'value'"; // records in DGV1 are
now filtered too!
dataGridView2.DataSource = bindingSource2;
dataGridView2.Columns["colname1"].DataPropertyName = "colname1db";
// etc. for every column from the DGV2

The thing I do not understand is why is the filter applied also to the
records shown in DGV1, while the filter is set for the binding source
assigned to DGV2...

TIA

With regards
nvx
 
M

Miha Markic [MVP C#]

Hi there,

When you bind to a datatable you actually bind its DefaultView which is a
DataView instance.
Thus, create a new DataView for that table and bind it to the other
BindingSource and you'll have two different views which will have different
filters.
 
N

nvx

Hello Miha,
thanks a lot for your advice. It works great... :)

Have a nice day...

With regards
nvx


Miha Markic [MVP C#] napsal:
Hi there,

When you bind to a datatable you actually bind its DefaultView which is a
DataView instance.
Thus, create a new DataView for that table and bind it to the other
BindingSource and you'll have two different views which will have different
filters.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

nvx said:
Hi,
is it possible to set different filters for two binding sources with
the same data source? If so, how? I tried to use this code:

BindingSource bindingSource1 = new BindingSource();
BindingSource bindingSource2 = new BindingSource();

bindingSource1.DataSource = myDataSet.Tables["tblname"];
dataGridView1.DataSource = bindingSource1;
dataGridView1.Columns["colname1"].DataPropertyName = "colname1db";
// etc. for every column from the DGV1

bindingSource2.DataSource = myDataSet.Tables["tblname"]; // i.e. the
same source as for the first bindingSource1
bindingSource2.Filter = "filtercol = 'value'"; // records in DGV1 are
now filtered too!
dataGridView2.DataSource = bindingSource2;
dataGridView2.Columns["colname1"].DataPropertyName = "colname1db";
// etc. for every column from the DGV2

The thing I do not understand is why is the filter applied also to the
records shown in DGV1, while the filter is set for the binding source
assigned to DGV2...

TIA

With regards
nvx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top