简体   繁体   中英

How can I use the same DataSource for multiple DataGridViews with a different filter applied on each?

I'm a beginner in C# and .NET am I am having the following problem (using .NET 4.5):

I have three DataGridViews which should display data from a xml file. Each DataGrid should filter its results, so they xml entries are spread over three DataGridViews.

I tried the following:

DataSet dataSet1 = new DataSet();
dataSet1.ReadXml('some-existing-file.xml');

DataTableCollection tables = dataSet1.Tables;
DataView view1 = new DataView(tables[0]);

BindingSource source1 = new BindingSource();
source1.DataSource = view1;
source1.Filter = "color = 'red'";
gridView1.DataSource = source1;

BindingSource source2 = new BindingSource();
source2.DataSource = view1;
source2.Filter = "color = 'white'";
gridView2.DataSource = source2;

BindingSource source3 = new BindingSource();
source3.DataSource = view1;
source3.Filter = "color = 'blue'";
gridView3.DataSource = source3;

But this does not work. All three GridViews use the last filter ('blue)'.

The XML looks like this (simplified):

<?xml version="1.0" encoding="utf-8"?>
<collection>
    <entry>
        <color>blue</color>
        <headline>Some headline</headline>
    </entry>
    [...]
</collection>

And is just filtering the same dataset the right way when I want to write changes back to the xml file?

Take three view as I think all three filters are applied on view and you get output showing the the result of last filter. You can later improve it, if it works

DataView view1 = new DataView(tables[0]);
DataView view2 = new DataView(tables[0]);
DataView view3 = new DataView(tables[0]);


BindingSource source1 = new BindingSource();
source1.DataSource = view1;
source1.Filter = "color = 'red'";
gridView1.DataSource = source1;

BindingSource source2 = new BindingSource();
source2.DataSource = view2;
source2.Filter = "color = 'white'";
gridView2.DataSource = source2;

BindingSource source3 = new BindingSource();
source3.DataSource = view3;
source3.Filter = "color = 'blue'";
gridView3.DataSource = source3;

It's possible that DataView has a publicly readable collection which is modified by the filter. When you are setting your data sources, supplying view1 in that way will provide a reference of the DataView, rather than a copy. This means that all of the filters are modifying the same viewable collection in the same DataView instance (which would explain why the last filter is the filter that is working).

I would suggest creating seperate instances/copies of the DataView for each filtering case. This would remain within your scope however, as they will still be referencing the same data that has been loaded from your xml file.

DataView view1 = new DataView(tables[0]); should be changed to prepare new DataView obj for each gridview as follows.

DataView view1 = new DataView(tables[0].Copy());
DataView view2 = new DataView(tables[0].Copy());
DataView view3 = new DataView(tables[0].Copy());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM