Click here to Skip to main content
15,116,516 members
Articles / Web Development / HTML
Posted 3 Aug 2005


148 bookmarked

Selecting Multiple checkboxes inside a GridView Control

Rate me:
Please Sign up or sign in to vote.
4.66/5 (83 votes)
3 Aug 20051 min read
This article describes how you can select checkboxes inside a GridView control.


GridView is a new data bound control introduced by Microsoft in Visual Studio .NET 2005. Most of the operations like sorting, paging and selecting items from the GridView are already built in and you can use it through the design view. In this article, I will explain how you can select single as well as all the checkboxes which are inside a GridView control.

Selecting Checkboxes inside the GridView Control

GridView has a CheckboxField column which maps the checkbox to a field in the database. In this article, we won't be using that, we will make a checkbox in a template column. Simply add a asp:checkbox control in the item template of the GridView control. If you are working with a DataGrid control and want the same functionality, then please check out my article: Selecting multiple checkboxes inside a DataGrid control.

The HTML code looks something like this:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
   AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PersonID" 
   DataSourceID="mySource" Width="366px" CellPadding="4" 
   ForeColor="#333333" GridLines="None">
   <asp:CommandField ShowSelectButton="True" />
   <asp:BoundField DataField="PersonID" HeaderText="PersonID" 
         InsertVisible="False" ReadOnly="True" SortExpression="PersonID" />
   <asp:BoundField DataField="Name" HeaderText="Name" 
                                       SortExpression="Name" />
   <asp:TemplateField HeaderText="Select">
       <asp:CheckBox ID="chkSelect" runat="server" />


 <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
 <PagerStyle BackColor="#FFCC66" ForeColor="#333333" 
                           HorizontalAlign="Center" />
 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 <AlternatingRowStyle BackColor="White" />

Now in the button click event, write this code:

// StringBuilder object
StringBuilder str = new StringBuilder();

// Select the checkboxes from the GridView control
for (int i = 0; i < GridView1.Rows.Count; i++)
  GridViewRow row = GridView1.Rows[i];
  bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;

  if (isChecked)
    // Column 2 is the name column

// prints out the result

The code above just iterates through the GridView and selects the checked checkboxes. Later, it appends the selected value to a StringBuilder object. In order to use StringBuilder, you will need to add the System.Text namespace.

Making a CheckAll Functionality

To add a check-all functionality in the GridView, simply add a HTML CheckBox to the header template of the checkbox column.

  <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" 
              runat="server" type="checkbox" />

SelectAllCheckboxes JavaScript method:

<script language=javascript>

 function SelectAllCheckboxes(spanChk){

   // Added as ASPX uses SPAN for checkbox
   var oItem = spanChk.children;
   var theBox= (spanChk.type=="checkbox") ? 
        spanChk : spanChk.children.item[0];

     if(elm[i].type=="checkbox" && 

This is it. I hope you like the article, happy coding!


  • 3rd August, 2005: Initial version


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.

I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

QuestionGood Article Pin
Yakup Ünyılmaz25-Jun-15 22:42
professionalYakup Ünyılmaz25-Jun-15 22:42 
GeneralGood Article Pin
Alireza_136210-Apr-13 21:33
MemberAlireza_136210-Apr-13 21:33 
QuestionI am not getting the check box value Pin
salamkudru16-Jan-13 21:23
Membersalamkudru16-Jan-13 21:23 
AnswerRe: I am not getting the check box value Pin
salamkudru17-Jan-13 0:17
Membersalamkudru17-Jan-13 0:17 
GeneralRe: I am not getting the check box value Pin
Anubha Bhatnagar22-Mar-13 3:14
MemberAnubha Bhatnagar22-Mar-13 3:14 
GeneralRe: I am not getting the check box value Pin
salamkudru24-Mar-13 19:58
Membersalamkudru24-Mar-13 19:58 
GeneralMy vote of 5 Pin
bluesathish20-Dec-12 18:23
Memberbluesathish20-Dec-12 18:23 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey9-Feb-12 22:05
professionalManoj Kumar Choubey9-Feb-12 22:05 
Questionhow can i count selected items and save them Pin
sparbery18-Aug-11 6:08
Membersparbery18-Aug-11 6:08 
GeneralThanks! Pin
fundmf5-Aug-11 21:23
Memberfundmf5-Aug-11 21:23 
Generalthx Pin
Uzhn15-Apr-11 21:44
MemberUzhn15-Apr-11 21:44 
GeneralUpdate Database Pin
pmosborne19-Jan-11 12:05
Memberpmosborne19-Jan-11 12:05 
GeneralGridview Pin
ragini123021-Nov-10 21:46
Memberragini123021-Nov-10 21:46 
GeneralMy vote of 1 Pin
adolfhardik4-Jun-10 0:21
Memberadolfhardik4-Jun-10 0:21 
QuestionHow to Invoke Child Listview controls' Edit and Update Events Pin
renil64912-May-10 6:07
Memberrenil64912-May-10 6:07 
QuestionMaster Page Pin
vicky_geek28-Apr-10 5:51
Membervicky_geek28-Apr-10 5:51 
AnswerRe: Master Page Pin
azamsharp28-Apr-10 5:59
Memberazamsharp28-Apr-10 5:59 
GeneralRe: Master Page Pin
vicky_geek28-Apr-10 20:17
Membervicky_geek28-Apr-10 20:17 
GeneralAutoPlay From the List Pin
vondon17-Feb-10 9:56
Membervondon17-Feb-10 9:56 
Generalit works great Pin
AnilMiLaN26-Nov-09 19:16
MemberAnilMiLaN26-Nov-09 19:16 
QuestionHow to bring Old state to the checkboxes of the gridview control when the postback occurs Pin
Rajeshgut14-Oct-09 4:40
MemberRajeshgut14-Oct-09 4:40 
AnswerRe: How to bring Old state to the checkboxes of the gridview control when the postback occurs Pin
azamsharp28-Apr-10 6:00
Memberazamsharp28-Apr-10 6:00 
QuestionCan you do this without forms Pin
cheesehead9118229-Sep-09 8:50
Membercheesehead9118229-Sep-09 8:50 
QuestionHow to select a checkbox with ctrl and click instead of click Pin
emily022823-Jun-09 10:15
Memberemily022823-Jun-09 10:15 
GeneralNice article Pin
Donsw18-Apr-09 11:21
MemberDonsw18-Apr-09 11:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.