WinForms,DataGridView:使用多个节点的属性过滤XML数据 [英] WinForms, DataGridView: Filtering XML data using attributes from multiple nodes

查看:107
本文介绍了WinForms,DataGridView:使用多个节点的属性过滤XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下格式的XML数据文件:

I have an XML data file of format similar to this:

<?xml version="1.0" standalone="yes"?>
<Root>
  <FirstLevel Id="1">
    <SecondLevel Id="1">
      <ThirdLevel Id="1">
        <DataElement Id="1" Data="hello" />
        <DataElement Id="2" Data="world" />
      </ThirdLevel>
      <ThirdLevel Id="2">
        <DataElement Id="1" Data="blablabla" />
        <DataElement Id="2" Data="blablabla" />
      </ThirdLevel>    
    </SecondLevel>
    <SecondLevel Id="2">
      <ThirdLevel Id="1">
        <DataElement Id="1" Data="asdf" />
        <DataElement Id="2" Data="qwerty" />
      </ThirdLevel>
      <ThirdLevel Id="2">
        <DataElement Id="1" Data="gggggg" />
        <DataElement Id="2" Data="dddddd" />
      </ThirdLevel>    
    </SecondLevel>
  </FirstLevel>
</Root>

我正在尝试使用绑定到此XML文件的DataGridView创建一个WinForms应用程序。并根据所选导航参数在网格中显示以下内容。例如,如果用户选择了所有的Id为1的FirstLevel,SecondLevel和ThirdLevel的导航,则只能显示以下2行,并且能够将任何更改写入XML:

And I'm trying to create a WinForms application using DataGridView that binds to this XML file. And displays the following in the grid, depending on selected navigation parameters. For example, if user selects navigation of FirstLevel, SecondLevel, and ThirdLevel with Id of 1 for all, only the following 2 rows should be displayed, with ability to write back any changes to XML:

Id   Data
----------
1    hello
2    world

到目前为止,我只能得到显示所有行(数据表):

So far, I can only get all the rows (datatables) to display:

Id  Data
---------
1   hello
2   world
1   blablabla
2   blablabla
1   asdf
2   qwerty
... etc

使用以下代码:

DataSet dataSet = new DataSet();
dataSet.ReadXML("Data.xml");
DataView dataView = new DataView(dataSet.Tables["DataElement"]);
BindingSource source = new BindingSource();
source.DataSource = dataView;
dataGridView1.DataSource = source;

如上所述,如何过滤我的数据,以便只显示2行?谢谢!

How can I filter my data so that only 2 rows are displayed, as described above? Thanks!

感谢康拉德为您的帮助!不过,我仍然试图找出如何在这三个层次之间进行导航,因为添加DataMember并不会增加过滤。因此,为了能够显示FirstLevel Id = 2,SecondLevel Id = 1,ThirdLevel Id = 5(或某事)的标准的数据,我必须将所有三个添加到:

Thanks Conrad for your help! However, I'm still trying to figure out how to "navigate" between these three levels, as adding a DataMember doesn't quite add the filtering. So to be able to, say, display data for criteria of FirstLevel Id = 2, SecondLevel Id = 1, ThirdLevel Id = 5 (or something), would I have to add all three to:

DataView dataView = new DataView(dataSet.Tables["FirstLevel_SecondLevel_ThirdLevel"]);

然后添加RowFilter,如下所示:

And then add RowFilter with something like:

dataView.RowFilter = "Id = '2'";

(但是这里的其他级别呢?)

(but what about other levels here?)

然后修改DataMember如下:

And then modify DataMember as follows:

source.DataMember = "FirstLevel_SecondLevel_ThirdLevel_DataElement";

这对我来说还不行。我真的只是反对这里的流程,这不是在WinForms中如何编辑XML数据?谢谢!

It doesn't quite work for me yet. Am I really just going against the flow here, and this is not how editing XML data should be approached in WinForms? Thanks!

推荐答案

更新
没有关系FirstLevel_SecondLevel_ThirdLevel不行您可以通过检查DataSet.Relations集合来找出存在哪些。

UPDATE There is no relation "FirstLevel_SecondLevel_ThirdLevel" which is why that doesn't work. You can find out which ones exist by inspecting the DataSet.Relations collection.

当您有多个级别时,您需要为每个级别创建一个视图。

When you have multiple levels as you do you need to create a view for each level.

        DataView firstDataView = new DataView(dataSet.Tables["FirstLevel"]);
        firstDataView.RowFilter = "Id = 1";


        DataView secondDataView  = firstDataView[0].CreateChildView("FirstLevel_SecondLevel");
        secondDataView.RowFilter = "Id = 2";

        DataView thirdDataView = secondDataView[0].CreateChildView("SecondLevel_ThirdLevel");
        DataView dataElement = thirdDataView[0].CreateChildView("ThirdLevel_DataElement");



        BindingSource source = new BindingSource();

        source.DataSource = dataElement;

        dataGridView1.DataSource = source;

这篇关于WinForms,DataGridView:使用多个节点的属性过滤XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆