简体   繁体   English

如何对多个DataGridView使用相同的DataSource,并对每个DataGridView应用不同的过滤器?

[英]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): 我是C#和.NET的初学者,遇到以下问题(使用.NET 4.5):

I have three DataGridViews which should display data from a xml file. 我有三个DataGridViews,它们应该显示xml文件中的数据。 Each DataGrid should filter its results, so they xml entries are spread over three DataGridViews. 每个DataGrid应该过滤其结果,因此它们的xml条目分布在三个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)'. 所有三个GridView使用最后一个过滤器(“蓝色”)。

The XML looks like this (simplified): XML看起来像这样(简化):

<?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? 当我想将更改写回到xml文件时,只是过滤正确的数据集是正确的方法吗?

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. DataView可能有一个公开可读的集合,该集合会被过滤器修改。 When you are setting your data sources, supplying view1 in that way will provide a reference of the DataView, rather than a copy. 设置数据源时,以这种方式提供view1将提供DataView的引用,而不是副本。 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). 这意味着所有过滤器都在同一DataView实例中修改相同的可见集合(这将解释为什么最后一个过滤器是起作用的过滤器)。

I would suggest creating seperate instances/copies of the DataView for each filtering case. 我建议为每个过滤案例创建单独的DataView实例/副本。 This would remain within your scope however, as they will still be referencing the same data that has been loaded from your xml file. 但是,这仍然在您的范围内,因为它们仍将引用从xml文件加载的相同数据。

DataView view1 = new DataView(tables[0]); should be changed to prepare new DataView obj for each gridview as follows. 应该进行如下更改以为每个gridview准备新的DataView obj。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#相同的数据源+多个DataGridViews =数据绑定问题? - C# Same DataSource + Multiple DataGridViews = Data Binding Issues? 如何最好地对源为Linq to SQL查询的DataGridViews进行筛选和排序? - How can I best filter and sort DataGridViews whose source is a Linq to SQL query? 如何在WPF中将多个组合框与一个数据源一起使用? - How can I use multiple combo boxes with one datasource in WPF? 当我在 C# 中有多个 datagridviews 时,如何获取当前选定的 datagridview 的名称? - How can I get the name of the current selected datagridview when I’ve multiple datagridviews in C#? 如何合并执行相同过滤但处理不同实体的多个自定义过滤器? - How can I merge multiple custom filter that do the same filtering but dealing with different entity? 如何在绑定到同一数据源的单独表单上获得两个控件以相互更新? - How can I get two controls on separate forms bound to the same datasource to update each other? 如何在2个DataGridViews中选择多行 - How to select multiple rows in 2 DataGridViews Datagridviews复选框单元格始终为null。 我该如何解决? - Datagridviews checkbox cells are always null. How can I fix it? 在第三个DataGridView中选择时,如何在2个DataGridViews中选择一行? - How can I select a row in 2 DataGridViews when selected in a third DataGridView? 如何:使用多个DataGridViews进行单选 - How to: Single Selection with Multiple DataGridViews
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM