Click here to Skip to main content
Click here to Skip to main content

Selecting multiple checkboxes inside a GridView control

, 3 Aug 2005
Rate this:
Please Sign up or sign in to vote.
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!


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
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, and
HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

My Blog:



Buy my iPhone app ABC Pop

Comments and Discussions

GeneralUpdate Database Pinmemberpmosborne19-Jan-11 12:05 
GeneralGridview Pinmemberragini123021-Nov-10 21:46 
GeneralMy vote of 1 Pinmemberadolfhardik4-Jun-10 0:21 
QuestionHow to Invoke Child Listview controls' Edit and Update Events Pinmemberrenil64912-May-10 6:07 
QuestionMaster Page Pinmembervicky_geek28-Apr-10 5:51 
AnswerRe: Master Page Pinmemberazamsharp28-Apr-10 5:59 
GeneralRe: Master Page Pinmembervicky_geek28-Apr-10 20:17 
GeneralAutoPlay From the List Pinmembervondon17-Feb-10 9:56 
Generalit works great PinmemberAnilMiLaN26-Nov-09 19:16 
QuestionHow to bring Old state to the checkboxes of the gridview control when the postback occurs PinmemberRajeshgut14-Oct-09 4:40 
AnswerRe: How to bring Old state to the checkboxes of the gridview control when the postback occurs Pinmemberazamsharp28-Apr-10 6:00 
QuestionCan you do this without forms Pinmembercheesehead9118229-Sep-09 8:50 
QuestionHow to select a checkbox with ctrl and click instead of click Pinmemberemily022823-Jun-09 10:15 
GeneralNice article PinmemberDonsw18-Apr-09 11:21 
GeneralPlz can u help PinmemberMehul_qaz16-Apr-09 4:52 
Generalnice article Pinmemberkanza azhar12-Apr-09 4:37 
QuestionCheckboxes and GridView paging [modified] Pinmemberboqboq2-Mar-09 2:58 
AnswerRe: Checkboxes and GridView paging PinmembercUbeindaclUb6-Sep-11 0:37 
QuestionBeautiful!...How can I have 2 gridviews on a page and NOT have them control each other? PinmemberBrian Fay9-Feb-09 12:51 
AnswerRe: Beautiful!...How can I have 2 gridviews on a page and NOT have them control each other? Pinmemberazamsharp11-Feb-09 8:50 
GeneralExcelente Mi Amigo. Sos pero jodido. PinmemberLuis Rangel4-Feb-09 7:34 
GeneralAA - Awesome Azam! Pinmemberplanetregin23-Dec-08 5:39 
Generalgood PinmemberMember 273824627-Aug-08 23:33 
GeneralThank you for the great article Pinmemberjitendra v patil22-Aug-08 1:11 
QuestionSessions questions PinmemberManigandanmax2cs16-Jul-08 1:31 
QuestionHow to count checked checkbox while paging in the gridview? [modified] PinmemberMember 408591513-Jun-08 2:46 
QuestionGridView.DataSource = table; is not working with checkboxes in the GridView PinmemberMember 459057921-Mar-08 23:57 
QuestionHow to mark CheckBox as checked in DataGrid according to condition taken from database Pinmemberabhijeet.musale13-Mar-08 4:22 
General multiple column each one has checkall checkbox, I got it Pinmemberab_dc21-Nov-07 3:16 
Questionmultiple column each one has checkall checkbox, how to??? Pinmemberab_dc21-Nov-07 2:28 
GeneralThe checkbox returns false even if it checked [modified] PinmemberNamrata111-Oct-07 0:37 
QuestionRe: The checkbox returns false even if it checked Pinmemberab_dc23-Oct-07 3:01 
GeneralVery Good Article Pinmemberumay26-Oct-07 20:35 
GeneralRe: Very Good Article Pinmemberab_dc28-Oct-07 2:06 
GeneralRe: Very Good Article Pinmemberriteshms27-Feb-08 0:45 
GeneralMulti column Check box list PinmemberT.Ashraf15-Aug-07 9:54 
GeneralUsing similar to update multiple rows Pinmemberbeish110-Aug-07 0:06 
GeneralBrilliant! Pinmemberthund3rstruck6-Jun-07 12:36 
GeneralThanks a lot azamsharp Pinmembertariqbk19-May-07 9:14 
GeneralStill problem in checkbox in gridview Pinmemberjigarchaudhari19-Jan-07 22:32 
GeneralRe: Still problem in checkbox in gridview Pinmembervijaymodi_8120-Jan-07 0:49 
GeneralRe: Still problem in checkbox in gridview Pinmemberazamsharp20-Jan-07 12:53 
GeneralRe: Still problem in checkbox in gridview Pinmemberjigarchaudhari21-Jan-07 19:32 
GeneralRe: Still problem in checkbox in gridview PinmemberNamrata111-Oct-07 1:02 
GeneralRe: Still problem in checkbox in gridview Pinmemberjigarchaudhari21-Jan-07 19:34 
QuestionI do checkbox selection in gridview but it gives me a false value Pinmemberjigarchaudhari19-Jan-07 1:48 
AnswerRe: I do checkbox selection in gridview but it gives me a false value Pinmembervijaymodi_8119-Jan-07 2:47 
QuestionRe: I do checkbox selection in gridview but it gives me a false value Pinmemberboyfromchina16-Jun-07 21:59 
Generalneed help "Can not check the checkbox control" Pinmemberjonathanwmk17-Jan-07 0:08 
QuestionUsing this code in page with MasterPage? PinmemberThienZ13-Nov-06 9:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 3 Aug 2005
Article Copyright 2005 by azamsharp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid