C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Binding XML Data to the GridView


Posted Date: 18 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: BharathiMember Level: Diamond    
Rating: Points: 8



This code sample binds XML data to a GridView control, by using DataTable.ReadXML() function.

The below code shows a simple example of binding the GridView control dynamically. The GridView control here is nested.

The code samples have .aspx page followed by the code-behind file.



<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="Grd" AutoGenerateColumns="false" OnRowDataBound="OnDataBound">
<Columns>
<asp:BoundField DataField="agent_cd" HeaderText="Agent Cd" />
<asp:TemplateField HeaderText ="Agent Details">
<ItemTemplate>
<asp:GridView ID="ChldGrid" runat="server">
<Columns>
<asp:BoundField DataField="agent_name" HeaderText="Agent Cd" />
<asp:BoundField DataField="address1" HeaderText="Address" />
<asp:BoundField DataField="city" HeaderText="City" />
<asp:BoundField DataField="state" HeaderText="State" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>




The following code is the code-behind C# file for the above .aspx page.


//Code for binding the GridView
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NestedGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
try
{
SqlConnection conn = new SqlConnection("");
conn.Open();

DataTable dt = new DataTable();
SqlDataAdapter ADA = new SqlDataAdapter("", conn);
ADA.Fill(dt);
Grd.DataSource = dt;
Grd.DataBind();
}
catch (Exception E)
{
string str = E.Message;
}
}
protected void OnDataBound(object sender , GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView Grd = (GridView)e.Row.FindControl("ChldGrid");
DataTable dt = new DataTable("TEST");
dt.Columns.Add("agent_cd");
dt.Columns.Add("agent_name");
dt.Columns.Add("address1");
dt.Columns.Add("city");
dt.Columns.Add("state");
dt.ReadXml(Server.MapPath("Data.xml"));
Grd.DataSource = dt;
Grd.DataBind();
}
}
}



Give below is a sample XML Content that can be used to test the program. Save this content as a .xml file.


<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<TEST>
<agent_cd>00000026</agent_cd>
<agent_name>TROPIC ICE INCORPORATED</agent_name>
<address1>2805 N COMMERCE PKWY</address1>
<city>MIRAMAR</city>
<state>FL</state>
</TEST>
</DocumentElement>


Attachments

  • Design Page (18867-18448-NestedGridView.aspx.txt)
  • Code Behind File (18867-18448-NestedGridView.aspx.cs.txt)
  • XML file (18867-18449-Data.xml)



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Bind XML data to nested GridView control in ASP.NET  .  Bind XML data to nested GridView control  .  Bind XML data to GridView control  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: changing grid view row colors in two different times
    Previous Resource: Nested Gridview with Checkbox Column
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design


    Contact Us    Privacy Policy    Terms Of Use