This way
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkBranchName" Text='<%# Eval("BranchName") %>' runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBoxList ID="chkSubBranchNames" runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<script src="Scripts/jQuery_1.8.3.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("[id*=chkBranchName]").click(function () {
if ($(this).is(":checked")) {
$(this).closest("tr").find("[id*=chkSubBranchNames]").find("input").attr('checked', 'checked');
}
else {
$(this).closest("tr").find("[id*=chkSubBranchNames]").find("input").removeAttr('checked');
}
});
$("[id*=chkSubBranchNames] input").click(function () {
if ($(this).closest("table").find("input:checked").length == $(this).closest("table").find("input").length) {
$(this).closest("table").closest("tr").find("[id*=chkBranchName]").attr('checked', 'checked');
}
else {
$(this).closest("table").closest("tr").find("[id*=chkBranchName]").removeAttr('checked');
}
});
});
</script>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGridView();
}
}
private void BindGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2]{
new DataColumn("BranchName", typeof(string)),
new DataColumn("SubBranchName", typeof(string)) });
dt.Rows.Add("Computers", "IT");
dt.Rows.Add("Computers", "CS");
dt.Rows.Add("Computers", "BSCIT");
dt.Rows.Add("Mechanical", "Marine");
dt.Rows.Add("Mechanical", "Aironotical");
dt.Rows.Add("Mechanical", "Railways");
ViewState["dt"] = dt;
this.GridView1.DataSource = dt.DefaultView.ToTable(true, "BranchName");
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBoxList lst = (CheckBoxList)(e.Row.FindControl("chkSubBranchNames"));
DataTable dt = (ViewState["dt"] as DataTable).Select("BranchName = '" + (e.Row.DataItem as DataRowView)["BranchName"].ToString() + "'").CopyToDataTable();
lst.DataSource = dt;
lst.DataTextField = "SubBranchName";
lst.DataValueField = "SubBranchName";
lst.DataBind();
}
}
using System.Data;