2
Answers

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

Photo of Hans Klose

Hans Klose

13y
1.4k
1
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)

1
Photo of brunda k
NA 1.5k 11.9k 13y

        DataSet set1 = new DataSet();
        string fileName = "data.xml";
        XPathExpression expr;
        
         DataGridView datagridview1 = new DataGridView();
         DataGridView datagridview2 = new DataGridView();

        public Form1()
        {
            InitializeComponent();

           datagridview1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(datagridview1_CellContentClick);
            //datagridview1.SelectionChanged += new EventHandler(datagridview1_SelectionChanged);

            datagridview1.SetBounds(100, 100, 200, 400);
            datagridview2.SetBounds(400, 100, 200, 400);
            this.Controls.Add(datagridview1);
            this.Controls.Add(datagridview2);
          }
         
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                 XPathDocument doc = new XPathDocument(fileName);
                 XPathNavigator nav = doc.CreateNavigator();
                 expr = nav.Compile("/Groups/Group/Name");
                 XPathNodeIterator iterator = nav.Select(expr);
                 DataTable dt = new DataTable();
                 dt.Columns.Add("Groups",typeof(string));
           
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
                    dt.Rows.Add(nav2.Value);
                       
                }
                  BindingSource source1 = new BindingSource();
                  source1.DataSource = dt;
                  datagridview1.DataSource = source1;
                 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

       }
      private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string value = datagridview1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                XPathDocument doc = new XPathDocument(fileName);
                XPathNavigator nav = doc.CreateNavigator();
                expr = nav.Compile("/Groups/Group[Name='" + value + "']/Item");
                XPathNodeIterator iterator = nav.Select(expr);
                DataTable dt = new DataTable();          
                dt.Columns.Add("Item", typeof(string));           
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
                    dt.Rows.Add(nav2.Value);

                }
                BindingSource source1 = new BindingSource();
                source1.DataSource = dt;
                datagridview2.DataSource = source1;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
       
        }

0
Photo of Hans Klose
NA 56 2.8k 13y
Thanks, with your solution only the Items of the selected group are shown.

But if I change or add a new Items in datagridview2 it disapears when I change to an other group and then come back to the group where the Item changed/added before.
Also before I could write the changed data back to xml with set1.WriteXml(filePath);