Hi Mehram,
You need to use OnDataBound event.
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="gvCustomers" runat="server" AllowPaging="false" AllowSorting="false" AutoGenerateColumns="false"
CssClass="Grid" OnRowDataBound="OnCustomerRowDataBound" DataKeyNames="CustomerID" ShowHeader="true" ShowFooter="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="plus" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrder" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid"
DataKeyNames="OrderID,Freight" ShowHeader="true" Width="100%" OnDataBound="OnOrderDataBound">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" />
<asp:BoundField DataField="Freight" HeaderText="Amount" DataFormatString="{0:N2}" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerID" HeaderText="ID" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<script type="text/javascript" src="https://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 width='32px'></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>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string query = "SELECT TOP 3 CustomerID,ContactName,City,Country FROM Customers";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
}
protected void OnCustomerRowDataBound(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;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string query = "SELECT OrderID,CustomerID,EmployeeID,Freight FROM Orders WHERE CustomerID = @CustomerID ORDER BY EmployeeID";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerID", customerID);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
gvOrders.DataSource = dt;
gvOrders.DataBind();
}
}
}
}
}
protected void OnOrderDataBound(object sender, EventArgs e)
{
GridView gvOrders = sender as GridView;
for (int rowIndex = gvOrders.Rows.Count - 1; rowIndex > 0; rowIndex--)
{
GridViewRow gvRow = gvOrders.Rows[rowIndex];
GridViewRow gvPreviousRow = gvOrders.Rows[rowIndex - 1];
if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
{
if (gvPreviousRow.Cells[0].RowSpan == 0)
{
if (gvRow.Cells[0].RowSpan == 0)
{
gvPreviousRow.Cells[0].RowSpan += 2;
}
else
{
gvPreviousRow.Cells[0].RowSpan = gvRow.Cells[0].RowSpan + 1;
}
gvRow.Cells[0].Visible = false;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim query As String = "SELECT TOP 3 CustomerID,ContactName,City,Country FROM Customers"
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub OnCustomerRowDataBound(ByVal sender As Object, ByVal 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)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim query As String = "SELECT OrderID,CustomerID,EmployeeID,Freight FROM Orders WHERE CustomerID = @CustomerID ORDER BY EmployeeID"
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@CustomerID", customerID)
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvOrders.DataSource = dt
gvOrders.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub OnOrderDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim gvOrders As GridView = TryCast(sender, GridView)
For rowIndex As Integer = gvOrders.Rows.Count - 1 To 1 Step -1
Dim gvRow As GridViewRow = gvOrders.Rows(rowIndex)
Dim gvPreviousRow As GridViewRow = gvOrders.Rows(rowIndex - 1)
If gvRow.Cells(0).Text = gvPreviousRow.Cells(0).Text Then
If gvPreviousRow.Cells(0).RowSpan = 0 Then
If gvRow.Cells(0).RowSpan = 0 Then
gvPreviousRow.Cells(0).RowSpan += 2
Else
gvPreviousRow.Cells(0).RowSpan = gvRow.Cells(0).RowSpan + 1
End If
gvRow.Cells(0).Visible = False
End If
End If
Next
End Sub
Screenshot