Binding GridView From XML Using ASP.Net

Introduction

There is often a need for applications to bind a Grid View from an Extensible Markup Language (XML) file that is widely used to communicate for cross-platform applications. To learn more about XML please refer to the many articles and books in this web site. Let us see the procedure for creating a XML file so students can understand it.
 
 Step 1:
 
 Create the project as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
  3. Give the project a name such as "BindGridfromxml" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - "Default.aspx" page.
  5. Drag and Drop one grid view to the Default.aspx page. Then the page will look such as follows.
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></title> 
 
</head>  
 <body bgcolor="silver"> 
     <form id="form1" runat="server">  

     <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"> 
         <AlternatingRowStyle BackColor="White" /> 
         <EditRowStyle BackColor="#2461BF" /> 
         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
         <RowStyle BackColor="#EFF3FB" /> 
         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
         <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
         <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
         <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
         <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
     </asp:GridView> 
     </form> 
 
</body> 
 
</html> 
Step 2:
  • Adding and creating XML file
To add a XML file from Visual Studio, right-click on Solution Explorer and choose XML file as in the following:
 
Add XML file
 
After adding the XML file it wll look such as follows:
  1. <?xml version="1.0" encoding="utf-8" ?> 
 Step 3:
 
Now let us add some records to the XML file as in the following:
<?xml version="1.0" encoding="utf-8" ?>  <Customer> 
  <Customerinfo> 
    <Name>Vithal Wadje</Name> 
    <city>Mumbai</city> 
    <Address>Sion, Mumbai</Address> 
 </Customerinfo> 
  <Customerinfo> 
    <Name>Sudhir Wadje</Name> 
    <city>Latur</city> 
    <Address>Latur</Address> 
  </Customerinfo> 
  <Customerinfo> 
    <Name>U D</Name> 
    <city>Mumbai</city> 
    <Address>DN,Road Mumbai</Address> 
  </Customerinfo> 
  <Customerinfo> 
    <Name>Anil Kumar</Name> 
    <city>Delhi</city>  z
    <Address>Nehru House</Address> 
  </Customerinfo>  
</Customer> 
In the preceding sample code the Custmorinfo tag represents the table name in the data table and Name, City and Address represents the column name. When you want to add records then you need to add a row within the Customerinfo tag every time.
Let us see the preceding XML file in the DataSet using the DataSet Visualizer as in the following:
 
 
 
 From the preceding example it's clear that the Customerinfo tag represents the table name in the data table and that Name, City and Address represents the column name.
 
Step 4:
 
 Now let us add the code into default.aspx.cs for the Page Load event to bind the Grid view as in the following:
 
Default.aspx.cs
using System; 
using System.Data;  using System.Data.SqlClient;    
public partial class _Default : System.Web.UI.Page 
{  
    DataSet ds = new DataSet();      protected void Page_Load(object sender, EventArgs e) 
    { 
        ds.ReadXml(Server.MapPath("~/Customer.xml")); 
        GridView1.DataSource = ds; 
        GridView1.DataBind();     
    }    

We are now done with the code, now run the application and the output will be as follows.
 
output Summary
 
I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode