articles

Home / DeveloperSection / Articles / Populating Grid Control from XML File

Populating Grid Control from XML File

mohan kumar7400 29-Nov-2011

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, please refer my article “Populating Grid Control, XML File and XSD File from Database”.

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 in my XML File.
  3.            Server.MapPath ( ) is used to get the exact location 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

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



Updated 07-Sep-2019
Having around 5 Years experience in .NET domain.

Leave Comment

Comments

Liked By