Bind XML data to asp gridview (C#) : GridView « Data Binding « ASP.Net






Bind XML data to asp gridview (C#)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Data"%>

<script runat="server">
    void Page_Load(Object sender, EventArgs e)
    {
        DataSet authorsDataSet;
        string filePath = Server.MapPath("Authors.xml");
        authorsDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        authorsDataSet.ReadXml(filePath);                    
        authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView;
        authorsGird.DataBind();
    }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Reading XML Data into a DataSet object </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView id="authorsGird" runat="server" 
            AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White" 
            HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
            <Columns>
                <asp:BoundField HeaderText="Last Name" DataField="lastName" />
                <asp:BoundField HeaderText="First Name" 
                    DataField="firstName" ItemStyle-HorizontalAlign="Right" />
            </Columns>           
        </asp:GridView>
    </div>
    </form>
</body>
</html>
           
       








Related examples in the same category