Populating Values From XML to a DataGridView and From a DataGridView to XML Using C#


Introduction

This document covers how to populate a DataGridView from XML and how to save DataGridView values back to XML.

Steps to configure.

Open Visual Studio and create a new Windows application.

PICS1.gif

Add a DataGridView to the form.

PICS2.gif

Create a XML file in your local drive (Say D:\ drive) with the following code.

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <
Table1>
      <Server>Server1</Server>
        <Database>Database1</Database
>
  </Table1>
  <Table1>
      <Server>Server2</Server>
      <Database>Database2</Database
>
  </Table1>
  <Table1>
      <Server>Server3</Server>
      <Database>Database3</Database
>
  </Table1>
</
NewDataSet>

Add the following code in the form load event.

//Populating databases value in dropdown

XmlDocument XMLDoc = new XmlDocument();

XMLDoc.Load(path);

//Give Node name here

XmlNodeList nodelist = XMLDoc.SelectNodes("NewDataSet/Table1");

foreach(XmlNode node in nodelist){

        comboBox1.Items.Add(node.SelectSingleNode("Database").InnerText);

}


Add a button "Add" below the DataGridView as shown below.

PICS3.gif
Add the following code in the button click event.

private void button1_Click(object sender, EventArgs e)
{
     //path of XML file
     string path = "D:\\Santhosh\\XMLFile1.xml";
     DataSet ds = new DataSet();
     DataTable dt = new DataTable();
     //Adding columns to datatable
    foreach (DataGridViewColumn col in dataGridView1.Columns)
    {
          dt.Columns.Add(col.DataPropertyName, col.ValueType);
    }
    //adding new rows
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
             DataRow row1 = dt.NewRow();
             for (int i = 0; i < dataGridView1.ColumnCount; i++)
             //if value exists add that value else add Null for that field
             row1[i] = (row.Cells[i].Value == null ? DBNull.Value : row.Cells[i].Value);
            dt.Rows.Add(row1);
    }
     //Copying from datatable to dataset
     ds.Tables.Add(dt);
     //writing new values to XML
     ds.WriteXml(path);
     MessageBox.Show("Successfully added ", "Success");
     this.Close()
 }
 
Run the solution. The following form should be displayed.

PICS7.gif
Now enter the new values into the DataGridView and click Add. Check that the values are being added.

PICS8.gif

Now check the new value is being added in the XML file.

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <
Table1>
      <Server>Server1</Server>
        <Database>Database1</Database
>
  </Table1>
  <Table1>
      <Server>Server2</Server>
      <Database>Database2</Database
>
  </Table1>
  <Table1>
      <Server>Server3</Server>
      <Database>Database3</Database
>
  </Table1>
</
NewDataSet>

Using the above concept to create a connection string from XML:

This can be used to configure a connection string at run time from XML. So the user can add his server name and database at XML and create connections at run time.

Now add a dropdown and a button above the DataGridView as shown below. Add the following code in the form load event to populate the databases in the dropdown as below.
 

/Populating databases value in dropdown
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(path);
//Give Node name here
XmlNodeList nodelist = XMLDoc.SelectNodes("NewDataSet/Table1");
foreach(XmlNode node in nodelist){
       comboBox1.Items.Add(node.SelectSingleNode("Database").InnerText);
}

PICS91.gif

PICS91.gif


Now add the following code to fetch the server name on selecting the database in the dropdown and click on Connect.


private
void button2_Click(object sender, EventArgs e)
{
      string path = "D:\\Santhosh\\XMLFile1.xml";
      string server="";
      XmlDocument XMLDoc = new XmlDocument();
      XMLDoc.Load(path);
     //Give Node name here
      XmlNodeList nodelist = XMLDoc.SelectNodes("NewDataSet/Table1");
      foreach (XmlNode node in nodelist)
      {
           if ((node.SelectSingleNode("Database").InnerText) == comboBox1.SelectedItem.ToString())
           {
                 server = node.SelectSingleNode("Server").InnerText;
           }

      }
}


Now using the database and server, create a new SqlConnection as usual.

Conclusion

By using the above code, the user can populate a DataGridView from XML and again save back the new values to the XML file. This will be very useful in creating a connection string also.


Recommended Free Ebook
Similar Articles