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">  
       </asp:GridView>  
     </asp:Content>
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.
    <Employees>  
      <Employee type="permanent">  
       <Name>John</Name>  
       <Id>E003674</Id>  
       <Age>34</Age>  
      </Employee>  
      <Employee type="contract">  
       <Name>Robin</Name>  
       <Id>E003675</Id>  
       <Age>25</Age>  
      </Employee>  
      <Employee type="permanent">  
       <Name>Tom</Name>  
       <Id>E003676</Id>  
       <Age>28</Age>  
      </Employee>  
      <Employee type="permanent">  
       <Name>Steve</Name>  
       <Id>E003677</Id>  
       <Age>28</Age>  
      </Employee>  
      <Employee type="contract">  
       <Name>Bren</Name>  
       <Id>E003678</Id>  
       <Age>38</Age>  
      </Employee>  
      <Employee type="permanent">  
       <Name>Javier</Name>  
       <Id>E003679</Id>  
       <Age>28</Age>  
      </Employee>  
     </Employees>  
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)  
         {  
           try  
           {  
             DataSet ds = new DataSet();  
             ds.ReadXml(Server.MapPath("~/Employee.xml"));  
             Grid_employee.DataSource = ds;  
             Grid_employee.DataBind();  
           }  
           catch (Exception)  
           {  
             throw;  
           }  
         }  
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">  
         <Columns>  
           <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" />  
         </Columns>  
       </asp:GridView>
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/




   Summary

      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