Hi mukesh1,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="grduser" runat="server" Width="100%" AutoGenerateColumns="false"
BorderColor="#F7F7F7" BorderWidth="1" GridLines="Horizontal">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div class="row">
<div class="col-md-5">
<asp:CheckBox ID="CheckBoxAll" AutoPostBack="true" runat="server" OnCheckedChanged="CheckBoxAll_CheckedChanged" />Select</div>
<div class="col-md-7">
User Name</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="row">
<div class="col-md-5">
<asp:CheckBox ID="CheckBox1" runat="server" Text="" />
</div>
<div class="col-md-7">
<asp:Label ID="lblname" runat="server" Text='<%# Eval("user_name") %>' Font-Size="Medium"></asp:Label>
<asp:Label ID="lbluser_id" runat="server" Visible="false" Text='<%# Eval("user_id") %>'
Font-Size="Medium"></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("user_name", typeof(string)),
new DataColumn("user_id",typeof(int)) });
dt.Rows.Add("John Hammond", 1);
dt.Rows.Add("Mudassar Khan", 2);
dt.Rows.Add("Suzanne Mathews", 3);
dt.Rows.Add("Robert Schidner", 4);
grduser.DataSource = dt;
grduser.DataBind();
}
}
protected void CheckBoxAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)grduser.HeaderRow.FindControl("CheckBoxAll");
foreach (GridViewRow row in grduser.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("CheckBox1");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
}
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int count = 0;
CheckBox ChkBoxHeader = (CheckBox)grduser.HeaderRow.FindControl("CheckBoxAll");
foreach (GridViewRow row in grduser.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("CheckBox1");
if (ChkBoxRows.Checked)
{
count++;
}
}
if (grduser.Rows.Count == count)
{
ChkBoxHeader.Checked = true;
}
else
{
ChkBoxHeader.Checked = false;
}
}
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("user_name", GetType(String)), New DataColumn("user_id", GetType(Integer))})
dt.Rows.Add("John Hammond", 1)
dt.Rows.Add("Mudassar Khan", 2)
dt.Rows.Add("Suzanne Mathews", 3)
dt.Rows.Add("Robert Schidner", 4)
grduser.DataSource = dt
grduser.DataBind()
End If
End Sub
Protected Sub CheckBoxAll_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ChkBoxHeader As CheckBox = CType(grduser.HeaderRow.FindControl("CheckBoxAll"), CheckBox)
For Each row As GridViewRow In grduser.Rows
Dim ChkBoxRows As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
If ChkBoxHeader.Checked = True Then
ChkBoxRows.Checked = True
Else
ChkBoxRows.Checked = False
End If
Next
End Sub
Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim count As Integer = 0
Dim ChkBoxHeader As CheckBox = CType(grduser.HeaderRow.FindControl("CheckBoxAll"), CheckBox)
For Each row As GridViewRow In grduser.Rows
Dim ChkBoxRows As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
If ChkBoxRows.Checked Then
count += 1
End If
Next
If grduser.Rows.Count = count Then
ChkBoxHeader.Checked = True
Else
ChkBoxHeader.Checked = False
End If
End Sub
Screenshot