将linq到xml查询的结果绑定到同一gridview

时间:2011-09-13 16:44:49

标签: c# datagridview linq-to-xml

如何将linq的结果与xml查询解析器绑定到同一gridview。我只能看到ZAxisCalib / query3的最后一个。

 string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {
            XDocument doc = XDocument.Load(fileName);
            var query = from x in doc.Descendants("XAxisCalib")
                        select new
                        {

                            MaxChild = x.Descendants("Max"),
                            MinChild = x.Descendants("Min")
                        };
            {
                var bs1 = new BindingSource { DataSource = query };

                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.AutoSize = true;

                dataGridView1.DataSource = bs1;
            }


            var query2 = from y in doc.Descendants("YAxisCalib")

                         select new
                         {

                             MaxChild = y.Descendants("Max"),
                             MinChild = y.Descendants("Min")

                         };


            var bs2 = new BindingSource { DataSource = query2 };

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;

            dataGridView1.DataSource = bs2;


            var query3 = from z in doc.Descendants("ZAxisCalib")

                         select new
                         {

                             MaxChild = z.Descendants("Max"),
                             MinChild = z.Descendants("Min")
                         };

            var bs3 = new BindingSource { DataSource = query3 };

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;

            dataGridView1.DataSource = bs3;

        }

2 个答案:

答案 0 :(得分:0)

试试这个:

string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {
            XDocument doc = XDocument.Load(fileName);
            var query = from x in doc.Descendants("XAxisCalib")
                        select new
                        {

                            MaxChild = x.Descendants("Max"),
                            MinChild = x.Descendants("Min")
                        };
          /*  {
                var bs1 = new BindingSource { DataSource = query };

                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.AutoSize = true;

                dataGridView1.DataSource = bs1;
            }*/


            var query2 = from y in doc.Descendants("YAxisCalib")

                         select new
                         {

                             MaxChild = y.Descendants("Max"),
                             MinChild = y.Descendants("Min")

                         };


          /*  var bs2 = new BindingSource { DataSource = query2 };

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;

            dataGridView1.DataSource = bs2;*/


            var query3 = from z in doc.Descendants("ZAxisCalib")

                         select new
                         {

                             MaxChild = z.Descendants("Max"),
                             MinChild = z.Descendants("Min")
                         };

            var bs3 = new BindingSource { DataSource = query.Union(query2.Union(query3)) };

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;

            dataGridView1.DataSource =bs3;

        }

我不编译它,它是一个快速草图

答案 1 :(得分:0)

您每次替换数据源。如果您只想连接三个查询的结果,可以使用:

var source = new BindingSource { DataSouce = query1.Concat(query2)
                                                   .Concat(query3) };
dataGridView1.DataSource = source;