Like his most previous posts, Scott Mitchell has written another simple and clean implementation of providing check/uncheck all functionality in GridView.
Checking All CheckBoxes in a GridView
Only thing his implementation was missing in article is placing CheckBox in header of GridView and using it to toggle rows CheckBoxes. I have made small modification in the existing code to provide this feature:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dirInfo As New DirectoryInfo(Request.PhysicalApplicationPath)
FileList.DataSource = dirInfo.GetFiles()
FileList.DataBind()
End If
'On every page visit we need to build up the CheckBoxIDs array
For Each gvr As GridViewRow In FileList.Rows
'Get a programmatic reference to the CheckBox control
Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)
ClientScript.RegisterArrayDeclaration("CheckBoxIDs", String.Concat("'", cb.ClientID, "'"))
Next
'--modified code
Dim checkAll As New CheckBox()
checkAll.Text = ""
checkAll.ID = "checkAll"
checkAll.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);")
FileList.HeaderRow.Cells(0).Controls.Add(checkAll)
End Sub

Hi Firoz,
you’ve done it with a postback. What about a client-script solution?
btw, a _very_ nice blog.
Thanks agarwaen.
This is actually client based solution of checking/unchecking rows checkboxs of grid. For server side toggling, modify code like this:
Dim checkAll As New CheckBox()
checkAll.Text = “”
checkAll.ID = “checkAll”
AddHandler checkAll.CheckedChanged, AddressOf toggleCheckBox
FileList.HeaderRow.Cells(0).Controls.Add(checkAll)
Protected Sub toggleCheckBox(ByVal sender As Object, ByVal e As System.EventArgs)
‘toggle all CheckBoxes here.
End Sub
Hi Agarwaen,
I have an Object DataSource populating my grid, which sometimes returns empty. How can I check for the presence of rows easily, to avoid uninstantiated object errors for the header checkbox??
hello
help me in programing c#
in vb -> Dim cb As CheckBox = CType(gvr.FindControl(“RowLevelCheckBox”), CheckBox)
in c# ?
thank you
by
MJD,
Here is the C# code for that:
CheckBox cb = ((CheckBox)(gvr.FindControl(“RowLevelCheckBox”)));
Hi Firoz,
ClientScript.RegisterArrayDeclaration does not exit within the System.Web.UI.UserControl. How can I add the CheckBox’s ID to the client-side CheckBoxIDs array within a UserControl?
Btw, this is a great enhancement to the original posting.
hey Its not working..i am confused with two sample..can you please elaborate it…Actaully i have checkboxex in gridview when i used a button to check them and uncheck it works fine but when to delete or save,checkbox.checked is false even its checked..can you u please help me out.
Rajsun, I think this is what happening in your case:
You probably setting Checked property of checkbox in Page_Load event. When user check those checkbox at browser, and clicks on Save button, same Page_Load code fires again and you loose the checked attribute of checkbox. You need to put your code inside -Not Page.IsPostBack- condition. Modify your code, something like this:
If Not Page.IsPostBack Then
Dim dirInfo As New DirectoryInfo(Request.PhysicalApplicationPath)
// Code which acually set Checked property
End If
Let me know if you are still facing this issue.
Here is the easier solution http://www.dotnetfunda.com/articles/article85.aspx
CheckBox chk=(CheckBox)Gridview.headerrow.findcontrol(“chk_header”);
above syntax for a header checkbox
below syntax for a item checkbox
CheckBox ch=(CheckBox)Gridview.rows[i].findcontrol(“chk_item”);
this syntax for a check uncheck checkbox but one problem in this formula if i have uncheck any checkbox from item template that time header template checkbox is checked that’s problem
tell me solution
just i know user unchecked the checkbox from item template that time header checkbox also unchecked how represent in coding
protected void chk_header_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)GridView1.HeaderRow.FindControl(“chk_header”);
if (chk.Checked == true)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ch = (CheckBox)GridView1.Rows[i].FindControl(“chk_item”);
ch.Checked = true;
}
}
}