---
title: "Populating Grid Control from XML File"  
description: "In this article, I am going to teach you, how to populate grid view control using a XML File as Data Source. For using Customer.xml as data source, pl"  
author: "mohan kumar"  
published: 2011-11-29  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/828/populating-grid-control-from-xml-file  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 1 minute  

---

# Populating Grid Control from XML File

In this article, I am going to teach you, how to [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) [grid view](https://www.mindstick.com/articles/53/data-grid-view) [control](https://yourviews.mindstick.com/view/83439/bipartisan-gun-control-bill-passed-in-us-after-decades) using a [XML File](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) as Data Source. For using Customer.xml as [data source](https://www.mindstick.com/forum/160659/how-do-you-create-a-custom-filter-to-filter-any-text-in-the-data-source-in-angularjs), please refer my article “Populating Grid Control, XML File and XSD File from [Database](https://www.mindstick.com/articles/71/how-to-create-xml-file-of-database-in-c-sharp)”.

##### Procedure:-

1. Add Customer.xml file using my article “Populating Data In XML And Grid” for populating grid control.
2. I have used ReadXml () to read the data [content](https://www.mindstick.com/articles/12369/cms-extension-for-magento-2-advanced-content-manager-2) in my XML File.
3. [Server.MapPath](https://www.mindstick.com/forum/2132/server-mappath-in-asp-dot-net) ( ) is used to get the exact [location](https://www.mindstick.com/news/2574/twitter-conveniently-reveals-a-location-sharing-policy-amid-elonjet-controversy) of the corresponding file.

Default.aspx:

\

```
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>Sample Program To
Populate Grid Control Using XML File</title></head><body>    <form id="form1" runat="server">    <div>        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="true"            GridLines="None" onpageindexchanging="GridView1_PageIndexChanging">            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />            <EditRowStyle BackColor="#999999" />            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />        </asp:GridView>    </div>    </form></body></html>
```

##### Default.aspx.cs

```
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.Sql;using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page{    SqlConnection conn
= new SqlConnection("Data
Source=CTS;uid=sa;pwd=home;Initial Catalog=northwind");     protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            loadMyGridFromXML();        }    }     public void loadMyGridFromXML()    {        conn.Open();        DataSet ds = new DataSet();        ds.ReadXml(Server.MapPath("Customers.xml"));        GridView1.DataSource
= ds;        GridView1.DataBind();        conn.Close();    }    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)    {        GridView1.PageIndex
= e.NewPageIndex;        loadMyGridFromXML();    }}
```

```
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.Sql;using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page{    SqlConnection conn= new SqlConnection("Data
Source=CTS;uid=sa;pwd=home;Initial Catalog=northwind");     protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            loadMyGridFromXML();        }    }     public void loadMyGridFromXML()    {        conn.Open();        DataSet ds = new DataSet();        ds.ReadXml(Server.MapPath("Customers.xml"));        GridView1.DataSource= ds;        GridView1.DataBind();        conn.Close();    }    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)    {        GridView1.PageIndex= e.NewPageIndex;        loadMyGridFromXML();    }} 
```

##### And Your Grid looks like :-

![Populating Grid Control from XML File](https://www.mindstick.com/mindstickarticle/92212a29-9954-4422-8d27-0d438c40e8fa/images/33545522-758c-4017-99af-5ad839ff7d36.png)

Happy Coding. Explore more and more as possible.

To Download this article source code,click the below download link. For your convinience, I have attached Northwind Database script with this zip file.

##### Server1:

http://www.4shared.com/file/S2s_SnNm/Populating_Grid_Control_from_X.html

Server2:\

http://www.mediafire.com/?r9887l3fxn0mbd2

\

---

Original Source: https://www.mindstick.com/articles/828/populating-grid-control-from-xml-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
