Header add

In this tutorial we discussed how to bind data in Grid view control of ASP .NET using XML file.

What is XML ?
XML is a markup language which is used to describe the data and transfer the data from one application to another application .XML is a language which is platform independent format so it can used on Internet "Heterogeneous environment" to transfer the data and to describe the data.

So let's create a application using Visual studio and create ASP .NET Web application:

Step-1: Open VS 2017 >> File >> New Project >> ASP .NET web application

Select Web-Forms
This is the application folder
When we run our application you can see the output like below; as the ASP .NET Web application provides default project.
But our agenda is bind the XML in Gridview so remove all the html file of "Default.aspx" and add the Gridview control as below.
     <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XMLBindGrid._Default" %>  
     <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
       <asp:GridView runat="server" ID="Grid_employee" CssClass="table table-bordered">  
Step-2: Now create the XML file and add the dummy data into XML file.
   (i)  Right Click Solution Folder >> Add New Item Select XML file
(ii)  Add the dummy data into the "Employee.xml" file as below.
      <Employee type="permanent">  
      <Employee type="contract">  
      <Employee type="permanent">  
      <Employee type="permanent">  
      <Employee type="contract">  
      <Employee type="permanent">  
Step-3: Open Default.aspx.cs file and bind the XML data in Grid-view. You can see in below code we declare a Dataset and fetch the Employee.xml using Server.MapPath and bind it into GridView.
    protected void Page_Load(object sender, EventArgs e)  
             DataSet ds = new DataSet();  
             Grid_employee.DataSource = ds;  
           catch (Exception)  
Step-4: Run the application and see the result. As per our XML data it bind into grid view a expected.
But you have noticed that the Header column is eaual to the XML file so if we want to change the header name then add the columns Header in Gridview control.
    <asp:GridView runat="server" ID="Grid_employee" CssClass="table table-bordered" AutoGenerateColumns="false">  
           <asp:BoundField DataField="Name" HeaderText="Employee Name" />  
           <asp:BoundField DataField="Id" HeaderText="Employee Id" />  
           <asp:BoundField DataField="Age" HeaderText="Employee Age" />  
           <asp:BoundField DataField="type" HeaderText="Employee Type" />  
Again run the application and see the Header name is changed as per our requirements.

You can see the Header Name has been changed.
</> The Source Code is available in Github.com/CoreProgramm/


      In this tutorial we discussed how to bind XML File Data in Grid-View Control in ASP .NET. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post