0
votes

I am kinda new with Using XML's with datagridviews. My problem is I have 2 datagrids, my first datagrid table would contain the following data also from the XML File.

empl_num |  Name
   1     |  Carl
   2     |  Mark
   3     |  Tanner

also, i have this XML file containing:

<Address>
  <Data1>
    <employee>
      <empl_num>1</empl_num>
      <name>Carl</name>
      <empl_num>2</empl_num>
      <name>Mark</name>
      <empl_num>3</empl_num>
      <name>Tanner</name>
    </employee>
  </Data1>
  <Data2>
    <Person>
      <EMPL_NUM>>100</EMPL_NUM>
      <NAME>Carl</NAME>
      <ID_NUM>1</ID_NUM>
      <IsRequired>0</IsRequired>
    </Person>
    <Person>
      <EMPL_NUM>200</EMPL_NUM>
      <NAME>Mark</NAME>
      <ID_NUM>2</ID_NUM>
      <IsRequired>0</IsRequired>
    </Person>
    <Person>
      <EMPL_NUM>300</EMPL_NUM>
      <NAME>Tanner</NAME>
      <ID_NUM>3</ID_NUM>
      <IsRequired>0</IsRequired>
    </Person>
 </Data2>
</Address>

May I ask a way on whenever i click the name carl from the datagridview table, it gets the xml data based on the ID_NUM and display it to the second datagridview table.(desired output shown example below).

example:

- datagridview1-                                -datagridview2-

empl_num |  Name                EMPL_NUM  |  Name  |  ID_NUM  |  IsRequired                  
   1     |  Carl                   100    |  Carl  |    1     |      0
   2     |  Mark
   3     |  Tanner
1
I was able to show the data in datagridview1 using datasets. i can't seem to figure out how to get the xml data of the clicked name to show in the datagridview2user1998735
sorry i forgot to include im using C# windows formsuser1998735

1 Answers

1
votes

Use data binding via binding source with foreign key.

// Fill data table (you should do it from xml file)
var dt1 = new DataTable("empl");
dt1.Columns.Add("empl_num");
dt1.Columns.Add("Name");
dt1.Rows.Add(1, "Carl");
dt1.Rows.Add(2, "Mark");
dt1.Rows.Add(3, "Tanner");

// Fill second data table
var dataSet = new DataSet();
using (var xmlReader = XmlReader.Create("test.xml"))
{
    xmlReader.ReadToFollowing("Data2");
    dataSet.ReadXml(xmlReader);
}

// Set a relation between two tables
dataSet.Tables.Add(dt1);
dataSet.Relations.Add("FK", dt1.Columns["empl_num"],
    dataSet.Tables[0].Columns["ID_NUM"]);


var bs1 = new BindingSource();
bs1.DataSource = dataSet;
bs1.DataMember = "empl";

var bs2 = new BindingSource();
bs2.DataSource = bs1;
bs2.DataMember = "FK"; // note to foreign key


// Bind sources to grids
dataGridView1.DataSource = bs1;
dataGridView2.DataSource = bs2;