Hi Vasanth057,
I have created sample that full-fill your requirement.
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
.Grid td
{
background-color: #A1DCF2;
color: black;
font-size: 10pt;
line-height: 200%;
}
.Grid th
{
background-color: #3AC0F2;
color: White;
font-size: 10pt;
line-height: 200%;
}
.ChildGrid td
{
background-color: #eee !important;
color: black;
font-size: 10pt;
line-height: 200%;
}
.ChildGrid th
{
background-color: #6C6C6C !important;
color: White;
font-size: 10pt;
line-height: 200%;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" CssClass="Grid"
DataKeyNames="CustomerID" OnRowDataBound="OnRowDataBound" ShowFooter="True" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="False" CssClass="ChildGrid"
ShowFooter="True" EnableModelValidation="True" OnRowDataBound="OnRowDataBoundChild">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="Order Id"></asp:BoundField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lblOrderDate" runat="server" Text='<%# Bind("OrderDate") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooterOrderDate" runat="server" Text="Total" Font-Bold="true" BackColor="Red"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Freight">
<ItemTemplate>
<asp:Label ID="lblFreight" runat="server" Text='<%# Bind("Freight", "{0:N2}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooterFreight" runat="server" Text="0" Font-Bold="true" BackColor="Black"
ForeColor="White"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ContactName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Garand Total" Font-Bold="true" BackColor="Red"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblGrandTotal" runat="server" Text="0" Font-Bold="true" BackColor="Black"
ForeColor="Wheat"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
C#
decimal total = 0;
decimal grandTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvCustomers.DataSource = GetData("select top 10 * from Customers");
gvCustomers.DataBind();
}
}
private static DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select top 3 * from Orders where CustomerId='{0}'", customerId));
gvOrders.DataBind();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
(e.Row.FindControl("lblGrandTotal") as Label).Text = grandTotal.ToString();
}
}
protected void OnRowDataBoundChild(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal freight = Convert.ToDecimal((e.Row.FindControl("lblFreight") as Label).Text);
total += freight;
grandTotal += freight;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
(e.Row.FindControl("lblFooterFreight") as Label).Text = total.ToString();
total = 0;
}
}
VB.Net
Private total As Decimal = 0
Private grandTotal As Decimal = 0
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
gvCustomers.DataSource = GetData("select top 10 * from Customers")
gvCustomers.DataBind()
End If
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = query
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim customerId As String = gvCustomers.DataKeys(e.Row.RowIndex).Value.ToString()
Dim gvOrders As GridView = TryCast(e.Row.FindControl("gvOrders"), GridView)
gvOrders.DataSource = GetData(String.Format("select top 3 * from Orders where CustomerId='{0}'", customerId))
gvOrders.DataBind()
End If
If e.Row.RowType = DataControlRowType.Footer Then
TryCast(e.Row.FindControl("lblGrandTotal"), Label).Text = grandTotal.ToString()
End If
End Sub
Protected Sub OnRowDataBoundChild(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim freight As Decimal = Convert.ToDecimal(TryCast(e.Row.FindControl("lblFreight"), Label).Text)
total += freight
grandTotal += freight
End If
If e.Row.RowType = DataControlRowType.Footer Then
TryCast(e.Row.FindControl("lblFooterFreight"), Label).Text = total.ToString()
total = 0
End If
End Sub
Screenshot
