Click here to Skip to main content
11,409,516 members (61,479 online)
Click here to Skip to main content

DataGrid CheckBox Select Multiple Rows Column

, 11 Apr 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
Allow users to select multiple records from your DataGrid. "CheckAll / UnCheck All" functionality on client side.

Sample Image - dgcheckboxcolumn.jpg


Multiple row selection in a web DataGrid is a usual requirement and the common and most elegant way to implement that is definitely using CheckBox(s). A checkbox column must of course be accompanied with a CheckAll / UnCheckAll functionality.


After searching this site, I was surprised not to find anything that suited this requirement completely. I did however find what I was looking for in different segments (acknowledged below). I decided to compile all what I needed in a reusable class / library (for reference). Following is the functionality provided:

  • Checkbox column for DataGrid.
  • CheckAll, UnCheckAll checkbox in the header without PostBack to the server.
  • Static method to register client side JavaScripts.

How to use

Download the supplied .cs file and add to your project. (If your project is in another language, create a new library project and add the supplied .cs to that project, compile the library, and attach the resulting .dll as a reference to your project).

Register a TagPrefix in your aspx page for the new column as follows:

<%@ Register TagPrefix="GridSelectCheckBox" Namespace="solCommon.Web.UI.DataGrid" 
                                                   Assembly="ProjectAssembly" %>

This is, of course, a snippet from my code, you can register any TagPrefix in your page. Should you change the namespace in the supplied .cs file, replace the Namespace attribute accordingly. The Assembly attribute must be your project/library assembly.

The next logical step is to include the column in your DataGrid. You may include the column at any location. Following adds the column at location 0, i.e. the first column:

<asp:datagrid ...>
    <GridSelectCheckBox:DGCheckBoxColumn />

Lastly, we use the exposed static method of DGCheckBoxColumn class to register client side JavaScripts for the CheckAll/UncheckAll feature. In the Page_Load method of your aspx page, add the following line:


You should replace "Form1" with the name of the form in which your DataGrid resides.

Your DataGrid is now ready.

You can get the selected indexes or the selected DataKeys by calling one of the exposed properties, i.e. DGCheckBoxColumn.SelectedIndexes and DGCheckBoxColumn.SelectedDataKeys. Let's say, in your button click event handler you try the following:

// since we know that our column in the grid is the first one ...
DGCheckBoxColumn dgchkbxCol = (DGCheckBoxColumn)myGrid.Columns[0];

// iterate through the selection
foreach (int i in dgchkbxCol.SelectedIndexes)
   // do something ...


I must thank the following two articles as I have used much of their contents:

Usage and Copyrights

You may freely use/modify the supplied file for your own usage, as long as you retain the acknowledgements presented above. Please don't forget to rate this article. Thanks and good luck.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Noman Nadeem
Web Developer
Saudi Arabia Saudi Arabia
No Biography provided

Comments and Discussions

Questionhow to select through checkboxes? PinmemberMember 466308727-Apr-08 21:57 
QuestionHow Select a row in Data Grid Pinmemberpatelmurtuza16-Mar-07 3:14 
Questionhow can i tell what my project/library assembly is Pinmemberinsignificant15-Mar-07 7:30 
GeneralIt is not a good idea PinmemberMember #2738631-Jan-07 22:56 
QuestionSpecified cast is not valid Pinmemberkrimsondon19-Dec-06 10:49 
QuestionHow to fetch info abt checked rows Pinmembermontysehra20-Nov-06 0:35 
It works fine, no problems but how to get the values contained in checked rows
QuestionCheck All button Does not work PinmemberStephen Noronha20-Oct-06 9:55 
AnswerRe: Check All button Does not work Pinmemberphantomvie30-Nov-07 0:21 
GeneralRe: Check All button Does not work PinmemberMember 279619825-Jan-10 2:54 
Generalmy problem:my datasource is a arrayList [modified] Pinmemberrengdongzwf11-Jun-06 21:28 
GeneralPaging Problem Pinmembermshariq10-Jun-06 19:07 
GeneralTo check the checkbox PinmemberOlog-hai17-May-06 9:43 
GeneralRe: To check the checkbox PinmemberTed Husted30-May-06 9:50 
GeneralRe: To check the checkbox PinmemberDonald Bittenbender17-Dec-08 12:23 
Generalcollecting information from a datagrid PinmemberJustmeNick30-Apr-06 8:15 
GeneralCheck All isn't working PinmemberBSegE12-Mar-06 6:54 
NewsProjectAssembly Cannot Load Pinmembertorome10-Mar-06 17:51 
GeneralAdding data PinmemberMiniDawg25-Jan-06 6:45 
GeneraldgchkbxCol.SelectedIndexes return empty Pinmemberlayoro5-Jan-06 17:15 
GeneralRe: dgchkbxCol.SelectedIndexes return empty PinmemberMiniDawg26-Jan-06 8:14 
JokeRe: dgchkbxCol.SelectedIndexes return empty Pinmemberlayoro26-Jan-06 15:25 
JokeRe: dgchkbxCol.SelectedIndexes return empty PinmemberMiniDawg27-Jan-06 3:50 
GeneralPage Errors...About Length Pinmemberekasal1-Dec-05 21:48 
GeneralClick the header checkbox that is not select all checkbox PinmemberVincent Tan E H24-Aug-05 1:19 
GeneralRe: Click the header checkbox that is not select all checkbox Pinmembererikwi26-Nov-05 2:08 
GeneralRe: Click the header checkbox that is not select all checkbox Pinmembererikwi26-Nov-05 2:22 
QuestionNice Code, but why not just use JavaScript? PinsussAnonymous22-Jul-05 17:16 
AnswerRe: Nice Code, but why not just use JavaScript? PinmemberSreejith Thathanattu23-Oct-06 0:54 
GeneralWriting to the Checkboxes PinmemberBrentLak27-Jun-05 5:52 
GeneralIt works Pinmemberldaniel9-Jun-05 5:27 
Questioncan believe PinmemberAkantos30-May-05 12:38 
GeneralPlease help to setup &quot;check all&quot; PinsussAnonymous21-Apr-05 4:59 
GeneralRe: Please help to setup &quot;check all&quot; PinmemberNoman Nadeem22-Apr-05 20:22 
GeneralRe: Please help to setup &quot;check all&quot; PinsussMichelle Tucker23-Apr-05 14:07 
GeneralRe: Please help to setup &quot;check all&quot; PinmemberNoman Nadeem23-Apr-05 19:30 
GeneralRe: Please help to setup &quot;check all&quot; PinsussCasual Jim30-May-05 1:57 
GeneralRe: Please help to setup &quot;check all&quot; Pinmemberjachyuen6-Jul-05 17:30 
GeneralRe: Please help to setup &quot;check all&quot; PinmemberMiniDawg25-Jan-06 10:12 
GeneralError PinmemberSel_13-Apr-05 17:29 
GeneralRe: Error PinmemberNoman Nadeem13-Apr-05 21:19 
Generalserious errors any idea what you are doing PinsussTereas preatur23-Apr-05 14:12 
GeneralRe: serious errors any idea what you are doing PinmemberNoman Nadeem23-Apr-05 19:33 
GeneralMetabuilders row selector column PinmemberSimon Capewell13-Apr-05 3:57 
Questionsuport? Pinmembertengtium13-Apr-05 0:39 
AnswerRe: suport? PinmemberNoman Nadeem13-Apr-05 7:45 
GeneralNot Bad... PinmemberBassam Abdul-Baki11-Apr-05 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
Web03 | 2.8.150414.5 | Last Updated 11 Apr 2005
Article Copyright 2005 by Noman Nadeem
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid