Ref:
Please refer this code
When the Child GridView checkBox is checked then Parent GridView CheckBox will be checked. And when only one checked CheckBox is checked inside the Child GridView and if its unchecked then parent GridView checkbox will be Unchecked too.
protected void Orders_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkOrder = (sender as CheckBox);
GridViewRow row = chkOrder.NamingContainer as GridViewRow;
GridViewRow parentRow = (row.Parent.Parent.NamingContainer as GridViewRow);
if (chkOrder.Checked)
{
(parentRow.FindControl("chkAll") as CheckBox).Checked = true;
}
else
{
GridView gv = (row.Parent).NamingContainer as GridView;
gv.AllowPaging = false;
int total = gv.Rows.Count;
int sum = 0;
foreach (GridViewRow gvRow in gv.Rows)
{
if ((gvRow.FindControl("chkCheck") as CheckBox).Checked == false)
{
sum++;
}
}
if (total == sum)
{
(parentRow.FindControl("chkAll") as CheckBox).Checked = false;
}
gv.AllowPaging = true;
}
}
Complete Sample
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%;
}
.Nested_ChildGrid td
{
background-color: #fff !important;
color: black;
font-size: 10pt;
line-height: 200%;
}
.Nested_ChildGrid th
{
background-color: #2B579A !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">
$(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>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="CustomerID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkAll" AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<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:CheckBox ID="chkCheck" runat="server" AutoPostBack="true" OnCheckedChanged="Orders_OnCheckedChanged" />
</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>
</form>
</body>
</html>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
C#
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkAll = (sender as CheckBox);
GridViewRow row = chkAll.NamingContainer as GridViewRow;
GridView gv = row.FindControl("gvOrders") as GridView;
foreach (GridViewRow gvRow in gv.Rows)
{
(gvRow.FindControl("chkCheck") as CheckBox).Checked = chkAll.Checked;
}
}
protected void Orders_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkOrder = (sender as CheckBox);
GridViewRow row = chkOrder.NamingContainer as GridViewRow;
GridViewRow parentRow = (row.Parent.Parent.NamingContainer as GridViewRow);
if (chkOrder.Checked)
{
(parentRow.FindControl("chkAll") as CheckBox).Checked = true;
}
else
{
GridView gv = (row.Parent).NamingContainer as GridView;
gv.AllowPaging = false;
int total = gv.Rows.Count;
int sum = 0;
foreach (GridViewRow gvRow in gv.Rows)
{
if ((gvRow.FindControl("chkCheck") as CheckBox).Checked == false)
{
sum++;
}
}
if (total == sum)
{
(parentRow.FindControl("chkAll") as CheckBox).Checked = false;
}
gv.AllowPaging = true;
}
}
Screenshot
![](https://i.imgur.com/rpzkWzk.png)
Rest of the code will remain same. Please refer the above article from where i have refer the code.