Skip to main content

How to Load Data in the DataGridView Using XML File and C#.

In this tutorial, I will teach you how to Load Data in the DataGridView Using XML File and C#. By doing this program, you will see that the output will be inside the DataGridView displaying the contents of an XML File. For you to have an idea of how this works, simply follow the procedure below.

Creating XML FIle

Go to solution explorer and right click the file name. After that select “Add” and hit “New Item”.
Choose the “XML File” and named it “person” then click “Add”.
Add data to the XML File.

  1. <NewDataSet>
  2. <person>
  3. <Name>Janno Palacios</Name>
  4. <Address>Kabankalan City</Address>
  5. </person>
  6. <person>
  7. <Name>Jeanniebe Palacios</Name>
  8. <Address>Kabankalan City</Address>
  9. </person>
  10. <person>
  11. <Name>Craig Palacios</Name>
  12. <Address>Kabankalan City</Address>
  13. </person>
  14. <person>
  15. <Name>Kenjie Palacios</Name>
  16. <Address>Kabankalan City</Address>
  17. </person>
  18. </NewDataSet>

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.

Step 2

Add a DataGridView and Button inside the Form just like shown below.

Step 3

Put the XML File in the resources folder by dragging it.

Step 4

Press F7 to open the code editor. In the code editor, create a method to get the data in the XML File and display it into the DataGridView.

  2. private void LoadPerson(DataGridView dtg)
  3. {
  4. DataSet dsCountries = new DataSet();
  5. dsCountries.ReadXml(new System.IO.StringReader(Properties.Resources.person));
  7. dtg.DataSource = dsCountries.Tables[0];
  8. }

Step 5

Write the following code to execute the method that you have created when the button is clicked.

  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. LoadPerson(dataGridView1);
  5. }

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.