Here I have created sample that full-fill your requirement.
I hope this will help you out.
HTML
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=imgOrdersShow]").each(function () {
if ($(this)[0].src.indexOf("minus") != -1) {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
$(this).next().remove();
}
});
$("[id*=imgProductsShow]").each(function () {
if ($(this)[0].src.indexOf("minus") != -1) {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
$(this).next().remove();
}
});
});
</script>
<asp:ImageButton ID="imgShowAllGrid" ImageUrl="~/images/plus.png" OnClick="Show_Hide_AllGrid"
CommandArgument="ShowAll" runat="server" Style="background-color: Red" />
<br />
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="CustomerID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgOrdersShow" runat="server" OnClick="Show_Hide_OrdersGrid"
ImageUrl="~/images/plus.png" CommandArgument="Show" />
<asp:Panel ID="pnlOrders" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" PageSize="5"
AllowPaging="true" OnPageIndexChanging="OnOrdersGrid_PageIndexChanging" CssClass="ChildGrid"
DataKeyNames="OrderId">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgProductsShow" runat="server" OnClick="Show_Hide_ProductsGrid"
ImageUrl="~/images/plus.png" CommandArgument="Show" />
<asp:Panel ID="pnlProducts" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" PageSize="2"
AllowPaging="true" OnPageIndexChanging="OnProductsGrid_PageIndexChanging" CssClass="Nested_ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="ProductId" HeaderText="Product Id" />
<asp:BoundField ItemStyle-Width="150px" DataField="ProductName" HeaderText="Product Name" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
<asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
</div>
Code
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 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;
}
}
}
}
}
protected void Show_Hide_OrdersGrid(object sender, EventArgs e)
{
ImageButton imgOrderShowHide = (sender as ImageButton);
GridViewRow row = (imgOrderShowHide.NamingContainer as GridViewRow);
if (imgOrderShowHide.CommandArgument == "Show")
{
row.FindControl("pnlOrders").Visible = true;
imgOrderShowHide.CommandArgument = "Hide";
imgOrderShowHide.ImageUrl = "~/images/minus.png";
string customerId = gvCustomers.DataKeys[row.RowIndex].Value.ToString();
GridView gvOrders = row.FindControl("gvOrders") as GridView;
BindOrders(customerId, gvOrders);
}
else
{
row.FindControl("pnlOrders").Visible = false;
imgOrderShowHide.CommandArgument = "Show";
imgOrderShowHide.ImageUrl = "~/images/plus.png";
}
}
protected void Show_Hide_AllGrid(object sender, EventArgs e)
{
ImageButton imgAllOrdersShowHide = (sender as ImageButton);
if (imgAllOrdersShowHide.CommandArgument == "ShowAll")
{
foreach (GridViewRow row in gvCustomers.Rows)
{
ImageButton imgCustomerShowHide = (ImageButton)row.FindControl("imgOrdersShow");
row.FindControl("pnlOrders").Visible = true;
imgCustomerShowHide.CommandArgument = "Hide";
imgCustomerShowHide.ImageUrl = "~/images/minus.png";
string customerId = gvCustomers.DataKeys[row.RowIndex].Value.ToString();
GridView gvOrders = row.FindControl("gvOrders") as GridView;
BindOrders(customerId, gvOrders);
foreach (GridViewRow orderRow in gvOrders.Rows)
{
GridView gvProducts = (GridView)(orderRow.FindControl("gvProducts"));
ImageButton imgProductShowHide = (ImageButton)orderRow.FindControl("imgProductsShow");
orderRow.FindControl("pnlProducts").Visible = true;
imgProductShowHide.CommandArgument = "Hide";
imgProductShowHide.ImageUrl = "~/images/minus.png";
int orderId = Convert.ToInt32((orderRow.NamingContainer as GridView).DataKeys[orderRow.RowIndex].Value);
BindProducts(orderId, gvProducts);
}
}
imgAllOrdersShowHide.CommandArgument = "HideAll";
imgAllOrdersShowHide.ImageUrl = "~/images/minus.png";
}
else
{
foreach (GridViewRow row in gvCustomers.Rows)
{
ImageButton imgCustomerShowHide = (ImageButton)row.FindControl("imgOrdersShow");
row.FindControl("pnlOrders").Visible = false;
imgCustomerShowHide.CommandArgument = "Show";
imgCustomerShowHide.ImageUrl = "~/images/plus.png";
}
imgAllOrdersShowHide.CommandArgument = "ShowAll";
imgAllOrdersShowHide.ImageUrl = "~/images/plus.png";
}
}
private void BindOrders(string customerId, GridView gvOrders)
{
gvOrders.ToolTip = customerId;
gvOrders.DataSource = GetData(string.Format("select * from Orders where CustomerId='{0}'", customerId));
gvOrders.DataBind();
}
protected void OnOrdersGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvOrders = (sender as GridView);
gvOrders.PageIndex = e.NewPageIndex;
BindOrders(gvOrders.ToolTip, gvOrders);
}
protected void Show_Hide_ProductsGrid(object sender, EventArgs e)
{
ImageButton imgProductShowHide = (sender as ImageButton);
GridViewRow row = (imgProductShowHide.NamingContainer as GridViewRow);
if (imgProductShowHide.CommandArgument == "Show")
{
row.FindControl("pnlProducts").Visible = true;
imgProductShowHide.CommandArgument = "Hide";
imgProductShowHide.ImageUrl = "~/images/minus.png";
int orderId = Convert.ToInt32((row.NamingContainer as GridView).DataKeys[row.RowIndex].Value);
GridView gvProducts = row.FindControl("gvProducts") as GridView;
BindProducts(orderId, gvProducts);
}
else
{
row.FindControl("pnlProducts").Visible = false;
imgProductShowHide.CommandArgument = "Show";
imgProductShowHide.ImageUrl = "~/images/plus.png";
}
}
private void BindProducts(int orderId, GridView gvProducts)
{
gvProducts.ToolTip = orderId.ToString();
gvProducts.DataSource = GetData(string.Format("SELECT ProductId, ProductName FROM Products WHERE ProductId IN (SELECT ProductId FROM [Order Details] WHERE OrderId = '{0}')", orderId));
gvProducts.DataBind();
}
protected void OnProductsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvProducts = (sender as GridView);
gvProducts.PageIndex = e.NewPageIndex;
BindProducts(int.Parse(gvProducts.ToolTip), gvProducts);
}
Screenshot
