check all/uncheck all check boxes in gridview using C# code


In this asp.net tutorial we will learn how to check all/uncheck all check boxes in gridview using C# code.

I can check all/uncheck all check boxes in gridview using javascript code that will work quite fine in all major web browsers such as Google Chrome, Internet Explorer, Opera and Mozilla FireFox but at the same time I also know most of the people want to do this using C# code. So this tutorial is for those people.

Let’s have a look over asp page

Checkall_uncheckall.aspx

<form id="form1" runat="server"> <div>


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="100%" GridLines="None" HeaderStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Top"><Columns>

<asp:TemplateField HeaderStyle-Width="5%">

<HeaderTemplate>
<asp:CheckBox ID="chkBox2" runat="server" Visible="true" AutoPostBack="true" OnCheckedChanged="chkBox2_CheckedChanged"/>

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox ID="chkBox" runat="server" Visible="true"/>

<asp:Label ID="lbl_Id" runat="server" Visible="false" Text="<%# Bind('ID') %>"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Roll No">

<ItemTemplate>

<asp:Label ID="lbl_Rollno" runat="server" Text='<%# Bind("RollNo")%>'>

</asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="First Name">

<ItemTemplate>

<asp:Label ID="lbl_Firstname" runat="server" Text='<%# Bind("FirstName")%>'>

</asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name">

<ItemTemplate>

<asp:Label ID="lbl_Lastname" runat="server" Text='<%# Bind("LastName")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="City"><ItemTemplate><asp:Label ID="lbl_City" runat="server" Text='<%# Bind("City")%>'></asp:Label></ItemTemplate></asp:TemplateField></Columns></asp:GridView>

</div>

</form>

Checkall_uncheckall.aspx.cs


protected void chkBox2_CheckedChanged(object sender, EventArgs e)
{

CheckBox chk = (CheckBox)GridView1.HeaderRow.FindControl("chkBox2");
if (chk.Checked)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkrow = (CheckBox)GridView1.Rows[i].FindControl("chkBox");
chkrow.Checked = true;
}

}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkrow = (CheckBox)GridView1.Rows[i].FindControl("chkbox");
chkrow.Checked = false;
}
}
}

//End Here

You have to make chkBox2_CheckedChanged(object sender, EventArgs e) function manually because in gridview you cannot double click on any control or select any control.

Note: - While making CheckedChanged function of any checkbox manually you must take care of one thing that function name must contain checkbox id and after check box id there must be _ and then you have to write CheckedChanged(object sender, EventArgs e) and it should be protected. Also please set AutoPostBack property of textbox to true so that this function can be executed.

I hope you will learn this tutorial, this tutorial is independent of any web browser compatibility because it is using C# code but if you want to use any web browser than you can try Google Chrome web browser. Google Chrome is official web browser of Google.

Anyhow again to the topic, this is the way to check all/uncheck all check boxes in gridview using C# code.

0 comments: