Bind GridView Control Using XML in ASP.Net

We will embed some data into the XML file and then we call that XML for binding purposes.

Initial Chamber

Step 1

Open Your Visual Studio 2010 and create an empty website, provide a suitable name (gridview_demo).

Step 2

In Solution Explorer you get your empty website. Add a web form, SQL Database and XML file by going like this.

For Web Form:

gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it gridview_demo.aspx.

For SQL Server Database:

gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add the database inside the App_Data_folder).

For XML File:

gridview_demo (your empty website) then right-click then select Add New Item -> XML File.

We are using a SQL Server Database because of the connection string that we need to provide for binding purposes. Otherwise there is no use of the database since we are not using the database table. Instead we are using a Dataset.

The following is the code of XML file:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <studentdetails>  
  3.   
  4.   <student>  
  5.     <id>01</id>  
  6.     <name>Nilesh</name>  
  7.     <city>Rajkot</city>  
  8.   </student>  
  9.   
  10.   <student>  
  11.     <id>02</id>  
  12.     <name>Purnima</name>  
  13.     <city>Ahmedabad</city>  
  14.   </student>  
  15.   
  16.   <student>  
  17.     <id>03</id>  
  18.     <name>Chandni</name>  
  19.     <city>Mumbai</city>  
  20.   </student>  
  21.   
  22.   <student>  
  23.     <id>04</id>  
  24.     <name>Rinku</name>  
  25.     <city>Pune</city>  
  26.   </student>  
  27.   
  28.   <student>  
  29.     <id>05</id>  
  30.     <name>Nilu</name>  
  31.     <city>Delhi</city>  
  32.   </student>  
  33.   
  34.   <student>  
  35.     <id>06</id>  
  36.     <name>Shailesh</name>  
  37.     <city>Nashik</city>  
  38.   </student>  
  39.   
  40.   
  41. </studentdetails>  
That is the student details data that we need to bind to the grid view, so that we can see the data at run time.

Design Chamber

Step 4

Open you gridview_demo.aspx file and write some code for designing our application.

Gridview_demo.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   
  13.             CellPadding="4" ForeColor="#333333" GridLines="None">  
  14.             <AlternatingRowStyle BackColor="White" />  
  15.             <Columns>  
  16.                 <asp:TemplateField HeaderText="Student Id">  
  17.                     <EditItemTemplate>  
  18.                         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>  
  19.                     </EditItemTemplate>  
  20.                     <ItemTemplate>  
  21.                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>  
  22.                     </ItemTemplate>  
  23.                 </asp:TemplateField>  
  24.                 <asp:TemplateField HeaderText="Student Name">  
  25.                     <EditItemTemplate>  
  26.                         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>  
  27.                     </EditItemTemplate>  
  28.                     <ItemTemplate>  
  29.                         <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>  
  30.                     </ItemTemplate>  
  31.                 </asp:TemplateField>  
  32.                 <asp:TemplateField HeaderText="Student City">  
  33.                     <EditItemTemplate>  
  34.                         <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>  
  35.                     </EditItemTemplate>  
  36.                     <ItemTemplate>  
  37.                         <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label>  
  38.                     </ItemTemplate>  
  39.                 </asp:TemplateField>  
  40.             </Columns>  
  41.             <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  42.             <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  43.             <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />  
  44.             <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />  
  45.             <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  46.             <SortedAscendingCellStyle BackColor="#FDF5AC" />  
  47.             <SortedAscendingHeaderStyle BackColor="#4D0000" />  
  48.             <SortedDescendingCellStyle BackColor="#FCF6C0" />  
  49.             <SortedDescendingHeaderStyle BackColor="#820000" />  
  50.         </asp:GridView>  
  51.     </div>  
  52.     </form>  
  53. </body>  
  54. </html>  
Here is what your design looks like:

design look

Code Chamber

Step 5

In the code chamber we will write some binding code so that our application shows the data from the XML file to the grid view.

Gridview_demo.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (!Page.IsPostBack)  
  15.         {  
  16.             refreshdata();  
  17.         }  
  18.     }  
  19.   
  20.   
  21.     public void refreshdata()  
  22.     {  
  23.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  24.        DataSet ds = new DataSet();  
  25.        ds.ReadXml(Server.MapPath("XMLFile.xml"));  
  26.         if (ds!= null && ds.HasChanges())  
  27.     {  
  28.             GridView1.DataSource=ds;  
  29.             GridView1.DataBind();  
  30.            
  31.     }  
  32.         else  
  33.         {  
  34.           
  35.                 GridView1.DataBind();  
  36.         }  
  37.       
  38.     }  
  39. }  
Output Chamber

Output

I hope you like this. Have a good day. Thank you for reading. 


Similar Articles