Hans Klose

Hans Klose

  • NA
  • 31
  • 3.3k

How can I show a selected part of an xml in a DataGridView?

Apr 11 2012 5:29 PM
Hi,

I read an XML into an dataset and show it in two dataGridViews. I coded a very small version of that xml into the code below.
In datagridview1 i show the Groups and in datagridview2 i show the items.

I want to show only the items from the group selected in datagridview1 (e.g. Items I1 and I2 if i click on Group1).

Can someon help me to get my code working as desired?

Here is my actually code:


 private void Form1_Load(object sender, EventArgs e)
        {
            DataSet set1 = new DataSet();


            string testXml =
               "<?xml version='1.0' encoding='UTF-8'?>" +
               "<Groups>" +
               "   <Group>" +
               "      <Name>Group1</Name>" +
               "      <Item>11</Item>" +
               "      <Item>12</Item>" +
               "   </Group>" +
               "   <Group>" +
               "      <Name>Group2</Name>" +
               "      <Item>21</Item>" +
               "   </Group>" +
               "</Groups>";

       
            StringReader reader = new StringReader(testXml);
            set1.ReadXml(reader);

            DataTableCollection tables = set1.Tables;
            DataView view1 = new DataView(tables[0]);
            DataView view2 = new DataView(tables[1]);

            DataGridView datagridview1 = new DataGridView();
            DataGridView datagridview2 = new DataGridView();

            datagridview1.SetBounds(100, 100, 200, 400);
            datagridview2.SetBounds(400, 100, 200, 400);
           
            this.Controls.Add(datagridview1);
            this.Controls.Add(datagridview2);

            BindingSource source1 = new BindingSource();
            source1.DataSource = view1;
            datagridview1.DataSource = source1;

            BindingSource source2 = new BindingSource();
            source2.DataSource = view2;
            datagridview2.DataSource = source2;

          //  source2.Filter = "Group_ID = 0";  // shows only items from Group1 (i1,i2)
        }

Answers (2)