In this code sample, I will explain how you can embed one DataGrid inside another DataGrid ASP.Net C# VB
<asp:DataGrid ID="dg" runat="server" DataKeyField="MemberId" AutoGenerateColumns="false" OnItemDataBound="dg_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="Id" HeaderText="Id"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Name" ItemStyle-HorizontalAlign="Left" ItemStyle-CssClass="blacktext">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.FirstName") %> <%# DataBinder.Eval(Container, "DataItem.LastName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Agency" HeaderText="Agency"></asp:BoundColumn>
<asp:BoundColumn DataField="IATANumber" HeaderText="IATANumber"></asp:BoundColumn>
<asp:BoundColumn DataField="AddedDate" HeaderText="Registration Date"></asp:BoundColumn>
<asp:BoundColumn DataField="Email" HeaderText="Email"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Fareinfo">
<ItemTemplate>
<asp:DataGrid ID="dgFareinfo" Runat="server" AllowPaging="False"
AllowSorting="False" AutoGenerateColumns="False"
ShowHeader="true" BorderColor="#DEBA84" CellPadding="3" CellSpacing="2"
BorderStyle="None" BackColor="#DEBA84" BorderWidth="1px">
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="PassengerName" HeaderText="PassengerName"></asp:BoundColumn>
<asp:BoundColumn DataField="TicketNumber" HeaderText="TicketNumber"></asp:BoundColumn>
<asp:BoundColumn DataField="Class" HeaderText="Class"></asp:BoundColumn>
<asp:BoundColumn DataField="Points" HeaderText="Points"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataAdapter adpgetData = new SqlDataAdapter("select * from LAN_members", configclass.DbConn);
DataSet dsGetData = new DataSet();
adpgetData.Fill(dsGetData);
if (dsGetData.Tables[0].Rows.Count > 0)
{
dg.DataSource = dsGetData.Tables[0];
dg.DataBind();
}
}
}
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string memid = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "memberid"));
SqlDataAdapter adpgetData = new SqlDataAdapter("select *, (select classname from Class where Class.ClassId=FareInfo.ClassType) as 'Class' from FareInfo where memberid=" + memid, configclass.DbConn);
DataSet dsf = new DataSet();
adpgetData.Fill(dsf);
if (dsf.Tables[0].Rows.Count > 0)
{
DataGrid dgFareinfo = ((DataGrid)e.Item.FindControl("dgFareinfo"));
dgFareinfo.DataSource = dsf.Tables[0];
dgFareinfo.DataBind();
}
}
}
<asp:DataGrid ID="dg" runat="server" DataKeyField="MemberId" AutoGenerateColumns="false" OnItemDataBound="dg_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="Id" HeaderText="Id"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Name" ItemStyle-HorizontalAlign="Left" ItemStyle-CssClass="blacktext">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.FirstName") %> <%# DataBinder.Eval(Container, "DataItem.LastName") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Agency" HeaderText="Agency"></asp:BoundColumn>
<asp:BoundColumn DataField="IATANumber" HeaderText="IATANumber"></asp:BoundColumn>
<asp:BoundColumn DataField="AddedDate" HeaderText="Registration Date"></asp:BoundColumn>
<asp:BoundColumn DataField="Email" HeaderText="Email"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Fareinfo">
<ItemTemplate>
<asp:DataGrid ID="dgFareinfo" Runat="server" AllowPaging="False"
AllowSorting="False" AutoGenerateColumns="False"
ShowHeader="true" BorderColor="#DEBA84" CellPadding="3" CellSpacing="2"
BorderStyle="None" BackColor="#DEBA84" BorderWidth="1px">
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="PassengerName" HeaderText="PassengerName"></asp:BoundColumn>
<asp:BoundColumn DataField="TicketNumber" HeaderText="TicketNumber"></asp:BoundColumn>
<asp:BoundColumn DataField="Class" HeaderText="Class"></asp:BoundColumn>
<asp:BoundColumn DataField="Points" HeaderText="Points"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataAdapter adpgetData = new SqlDataAdapter("select * from LAN_members", configclass.DbConn);
DataSet dsGetData = new DataSet();
adpgetData.Fill(dsGetData);
if (dsGetData.Tables[0].Rows.Count > 0)
{
dg.DataSource = dsGetData.Tables[0];
dg.DataBind();
}
}
}
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string memid = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "memberid"));
SqlDataAdapter adpgetData = new SqlDataAdapter("select *, (select classname from Class where Class.ClassId=FareInfo.ClassType) as 'Class' from FareInfo where memberid=" + memid, configclass.DbConn);
DataSet dsf = new DataSet();
adpgetData.Fill(dsf);
if (dsf.Tables[0].Rows.Count > 0)
{
DataGrid dgFareinfo = ((DataGrid)e.Item.FindControl("dgFareinfo"));
dgFareinfo.DataSource = dsf.Tables[0];
dgFareinfo.DataBind();
}
}
}