简体   繁体   English

c#按属性过滤xml文件并在datagridview中显示

[英]c# filter xml file by attribute and show in datagridview

I have looked and can't find the answer, I'm sure I'm just not looking for the right keywords, lol. 我已经找到了答案,但找不到答案,我确定我只是在寻找正确的关键字,哈哈。

I have an XML file that I display in a datagridview, this all works fine. 我有一个显示在datagridview中的XML文件,一切正常。 I'm trying to implant where I can click a button and 'reload' the datagridview with only certain parts of the xml file. 我试图植入我可以单击按钮的位置,并仅使用xml文件的某些部分“重新加载” datagridview。

I will describe better. 我会描述得更好。 Just bear with me, I'm not the best at explaining. 只是忍受我,我不是最擅长解释。

This is the layout of my xml file. 这是我的xml文件的布局。

<?xml version="1.0"?>
<Movies>
  <Name Name="Saw" Type="Horror" Year="2004-10-29" Overview="Overview">Saw</Name>
  <Name Name="Saw II" Type="Horror" Year="2005-10-28" Overview="Overview">Saw II</Name>
  <Name Name="Speed" Type="Action" Year="1994-06-10" Overview="Overview">Speed</Name>
  <Name Name="Batman Begins" Type="Action" Year="2005-06-17" Overview="Overview">Batman Begins</Name>
</Movies>

When I display normally I have it in a grid view like so. 当我正常显示时,我会像这样在网格视图中显示它。 I had to upload image elsewhere the site wouldn't let me put it here. 我不得不将图像上传到其他地方,该网站不允许我将其放在此处。 enter link description here 在此处输入链接说明

Now what I'm trying to do is make is where when I click a button I can display only certain Movie Types. 现在,我想做的是make,当我单击按钮时,我只能显示某些电影类型。 Say for example, click a button and only display movies that are "Action". 例如,单击一个按钮,仅显示“动作”的电影。

I have found information on this and got where I can select only the actions. 我已找到有关此方面的信息,并在此处只能选择操作。 I got them to pop up in messageboxs' 我让他们弹出在消息框中

                XmlDocument xml = new XmlDocument();
            xml.Load("movie.xml");

            XmlNodeList xnList = xml.SelectNodes("/Movies/Name[@Type='Action']");
            foreach (XmlNode xn in xnList)
            {
                MessageBox.Show(xn.InnerText);
            }

This actually shows say from the image the 2 movies that are action. 实际上,从图像中可以看出正在播放的2部电影。 But I can't figure out how to reload the datagridview with only these entries, and prefer with all information as before. 但是我无法弄清楚如何仅使用这些条目来重新加载datagridview,并且更喜欢像以前一样使用所有信息。

If I didn't explain clearly please say so, as I said before I know I"m not the best at explaining things. 如果我没有清楚地解释,请这样说,就像我在知道我不是最擅长解释事物之前所说的那样。

One way is to load the xml into a DataTable. 一种方法是将xml加载到DataTable中。 Then you can use its in-memory filtering capability to filter the grid. 然后,您可以使用其内存中过滤功能来过滤网格。

DataSet ds = new DataSet();
ds.ReadXml("Movies.xml");
this.dataGridView.DataSource = ds.Tables[0];

Then you can filter it like this 然后您可以像这样过滤

private void FilterByType(string type)
{
            var dataView = ((DataTable) this.dataGridView.DataSource).DefaultView;
            dataView.RowFilter = "(Type = '" + type + "')";
}

To filter by substring use this syntax 要按子字符串过滤,请使用以下语法

dataView.RowFilter = "(Type LIKE *'" + substring + "*')";

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

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