Hi ucrhlyn,
Check this example. Now please take its reference and correct your code.
In this example i have used ProductID to show SubTotal. You need to change as per your table structure.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th scope="col"> </th>
<th scope="col" style="width: 150px">Contact Name</th>
<th scope="col" style="width: 150px">City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<img alt="" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:Repeater ID="rptOrders" runat="server" OnItemDataBound="OnItemDataBound1">
<HeaderTemplate>
<table class="ChildGrid" cellspacing="0" rules="all" border="1">
<tr>
<th scope="col"> </th>
<th scope="col" style="width: 150px">Order Id</th>
<th scope="col" style="width: 150px">Date</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<img alt="" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrderDetails" runat="server" AutoGenerateColumns="false"
OnRowDataBound="OnRowDataBound" OnDataBound="OnDataBound" OnRowCreated="OnRowCreated" >
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="OrderId" />
<asp:BoundField DataField="UnitPrice" HeaderText="Price" DataFormatString="{0:N2}" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
<td><asp:Label ID="lblOrderId" runat="server" Text='<%# Eval("OrderId") %>' /></td>
<td><asp:Label ID="lblOrderDate" runat="server" Text='<%# Eval("OrderDate","{0:dd/MM/yyyy}") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
<asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("CustomerId") %>' />
</td>
<td><asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' /></td>
<td><asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("click", "[src*=plus]", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("body").on("click", "[src*=minus]", function () {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptCustomers.DataSource = GetData("SELECT TOP 3 * FROM Customers");
rptCustomers.DataBind();
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
rptOrders.DataSource = GetData(string.Format("SELECT TOP 3 * FROM Orders WHERE CustomerId='{0}'", customerId));
rptOrders.DataBind();
}
}
protected void OnItemDataBound1(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string orderId = (e.Item.FindControl("lblOrderId") as Label).Text;
GridView gvOrderDetails = e.Item.FindControl("gvOrderDetails") as GridView;
gvOrderDetails.DataSource = GetData(string.Format("SELECT TOP 3 * FROM [Order Details] WHERE OrderId='{0}'", orderId));
gvOrderDetails.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal price = Convert.ToDecimal(e.Row.Cells[1].Text.Trim());
decimal quantity = Convert.ToDecimal(e.Row.Cells[2].Text.Trim());
(e.Row.FindControl("lblTotal") as Label).Text = (price * quantity).ToString();
}
}
int currentId = 0;
decimal subTotal = 0;
int subTotalRowIndex = 0;
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
GridView GridView1 = sender as GridView;
subTotal = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
int productID = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["ProductID"]);
decimal price = Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["UnitPrice"]);
if (productID != currentId)
{
if (e.Row.RowIndex > 0)
{
for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
{
subTotal += Convert.ToDecimal(GridView1.Rows[i].Cells[2].Text);
}
this.AddTotalRow(GridView1, "Sub Total", subTotal.ToString("N2"));
subTotalRowIndex = e.Row.RowIndex;
}
currentId = productID;
}
}
}
protected void OnDataBound(object sender, EventArgs e)
{
GridView GridView1 = sender as GridView;
if (GridView1.Rows.Count > 0)
{
for (int i = subTotalRowIndex; i < GridView1.Rows.Count; i++)
{
subTotal += Convert.ToDecimal(GridView1.Rows[i].Cells[2].Text);
}
this.AddTotalRow(GridView1, "Sub Total", subTotal.ToString("N2"));
subTotalRowIndex = 0;
}
}
private void AddTotalRow(GridView GridView1, string labelText, string value)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
row.Cells.AddRange(new TableCell[4] {
new TableCell (), //Empty Cell
new TableCell (), //Empty Cell
new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right},
new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } });
GridView1.Controls[0].Controls.Add(row);
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
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;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
rptCustomers.DataSource = GetData("SELECT TOP 3 * FROM Customers")
rptCustomers.DataBind()
End If
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim customerId As String = (TryCast(e.Item.FindControl("hfCustomerId"), HiddenField)).Value
Dim rptOrders As Repeater = TryCast(e.Item.FindControl("rptOrders"), Repeater)
rptOrders.DataSource = GetData(String.Format("SELECT TOP 3 * FROM Orders WHERE CustomerId='{0}'", customerId))
rptOrders.DataBind()
End If
End Sub
Protected Sub OnItemDataBound1(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim orderId As String = (TryCast(e.Item.FindControl("lblOrderId"), Label)).Text
Dim gvOrderDetails As GridView = TryCast(e.Item.FindControl("gvOrderDetails"), GridView)
gvOrderDetails.DataSource = GetData(String.Format("SELECT TOP 3 * FROM [Order Details] WHERE OrderId='{0}'", orderId))
gvOrderDetails.DataBind()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim price As Decimal = Convert.ToDecimal(e.Row.Cells(1).Text.Trim())
Dim quantity As Decimal = Convert.ToDecimal(e.Row.Cells(2).Text.Trim())
TryCast(e.Row.FindControl("lblTotal"), Label).Text = (price * quantity).ToString()
End If
End Sub
Private currentId As Integer = 0
Private subTotal As Decimal = 0
Private subTotalRowIndex As Integer = 0
Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim GridView1 As GridView = TryCast(sender, GridView)
subTotal = 0
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dt As DataTable = (TryCast(e.Row.DataItem, DataRowView)).DataView.Table
Dim productID As Integer = Convert.ToInt32(dt.Rows(e.Row.RowIndex)("ProductID"))
Dim price As Decimal = Convert.ToDecimal(dt.Rows(e.Row.RowIndex)("UnitPrice"))
If productID <> currentId Then
If e.Row.RowIndex > 0 Then
For i As Integer = subTotalRowIndex To e.Row.RowIndex - 1
subTotal += Convert.ToDecimal(GridView1.Rows(i).Cells(2).Text)
Next
Me.AddTotalRow(GridView1, "Sub Total", subTotal.ToString("N2"))
subTotalRowIndex = e.Row.RowIndex
End If
currentId = productID
End If
End If
End Sub
Protected Sub OnDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim GridView1 As GridView = TryCast(sender, GridView)
If GridView1.Rows.Count > 0 Then
For i As Integer = subTotalRowIndex To GridView1.Rows.Count - 1
subTotal += Convert.ToDecimal(GridView1.Rows(i).Cells(2).Text)
Next
Me.AddTotalRow(GridView1, "Sub Total", subTotal.ToString("N2"))
subTotalRowIndex = 0
End If
End Sub
Private Sub AddTotalRow(ByVal GridView1 As GridView, ByVal labelText As String, ByVal value As String)
Dim row As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
row.BackColor = ColorTranslator.FromHtml("#F9F9F9")
row.Cells.AddRange(New TableCell(3) {
New TableCell(),
New TableCell(),
New TableCell With {.Text = labelText, .HorizontalAlign = HorizontalAlign.Right},
New TableCell With {.Text = value, .HorizontalAlign = HorizontalAlign.Right}})
GridView1.Controls(0).Controls.Add(row)
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = query
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Screenshot