Click here to Skip to main content
11,648,094 members (67,274 online)
Click here to Skip to main content

DataGrid CheckBox Select Multiple Rows Column

, 11 Apr 2005 CPOL 209.2K 2.8K 84
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

Introduction

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.

Outline

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 ...>
...
    <Columns>
    <GridSelectCheckBox:DGCheckBoxColumn />
    ...
    </Columns>
...
</asp:datagrid>

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:

DGCheckBoxColumn.RegisterClientCheckEvents(this,"Form1");

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 ...
}

Acknowledgements

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.

License

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

Share

About the Author

Noman Nadeem
Web Developer
Saudi Arabia Saudi Arabia
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionhow to select through checkboxes? Pin
Member 466308727-Apr-08 20:57
memberMember 466308727-Apr-08 20:57 
QuestionHow Select a row in Data Grid Pin
patelmurtuza16-Mar-07 2:14
memberpatelmurtuza16-Mar-07 2:14 
Questionhow can i tell what my project/library assembly is Pin
insignificant15-Mar-07 6:30
memberinsignificant15-Mar-07 6:30 
GeneralIt is not a good idea Pin
Member #2738631-Jan-07 21:56
memberMember #2738631-Jan-07 21:56 
QuestionSpecified cast is not valid Pin
krimsondon19-Dec-06 9:49
memberkrimsondon19-Dec-06 9:49 
QuestionHow to fetch info abt checked rows Pin
montysehra19-Nov-06 23:35
membermontysehra19-Nov-06 23:35 
QuestionCheck All button Does not work Pin
Stephen Noronha20-Oct-06 8:55
memberStephen Noronha20-Oct-06 8:55 
AnswerRe: Check All button Does not work Pin
phantomvie29-Nov-07 23:21
memberphantomvie29-Nov-07 23:21 
GeneralRe: Check All button Does not work Pin
Member 279619825-Jan-10 1:54
memberMember 279619825-Jan-10 1:54 
i'd suggest you to replace that bolded code with

if (wc is CheckBox)

and process checkboxess only if that condition is true. You can avoid also try-catch this way
Generalmy problem:my datasource is a arrayList [modified] Pin
rengdongzwf11-Jun-06 20:28
memberrengdongzwf11-Jun-06 20:28 
GeneralPaging Problem Pin
mshariq10-Jun-06 18:07
membermshariq10-Jun-06 18:07 
GeneralTo check the checkbox Pin
Olog-hai17-May-06 8:43
memberOlog-hai17-May-06 8:43 
GeneralRe: To check the checkbox Pin
Ted Husted30-May-06 8:50
memberTed Husted30-May-06 8:50 
GeneralRe: To check the checkbox Pin
Donald Bittenbender17-Dec-08 11:23
memberDonald Bittenbender17-Dec-08 11:23 
Generalcollecting information from a datagrid Pin
JustmeNick30-Apr-06 7:15
memberJustmeNick30-Apr-06 7:15 
GeneralCheck All isn't working Pin
BSegE12-Mar-06 5:54
memberBSegE12-Mar-06 5:54 
NewsProjectAssembly Cannot Load Pin
torome10-Mar-06 16:51
membertorome10-Mar-06 16:51 
GeneralAdding data Pin
MiniDawg25-Jan-06 5:45
memberMiniDawg25-Jan-06 5:45 
GeneraldgchkbxCol.SelectedIndexes return empty Pin
layoro5-Jan-06 16:15
memberlayoro5-Jan-06 16:15 
GeneralRe: dgchkbxCol.SelectedIndexes return empty Pin
MiniDawg26-Jan-06 7:14
memberMiniDawg26-Jan-06 7:14 
JokeRe: dgchkbxCol.SelectedIndexes return empty Pin
layoro26-Jan-06 14:25
memberlayoro26-Jan-06 14:25 
JokeRe: dgchkbxCol.SelectedIndexes return empty Pin
MiniDawg27-Jan-06 2:50
memberMiniDawg27-Jan-06 2:50 
GeneralPage Errors...About Length Pin
ekasal1-Dec-05 20:48
memberekasal1-Dec-05 20:48 
GeneralClick the header checkbox that is not select all checkbox Pin
Vincent Tan E H24-Aug-05 0:19
memberVincent Tan E H24-Aug-05 0:19 
GeneralRe: Click the header checkbox that is not select all checkbox Pin
erikwi26-Nov-05 1:08
membererikwi26-Nov-05 1:08 
GeneralRe: Click the header checkbox that is not select all checkbox Pin
erikwi26-Nov-05 1:22
membererikwi26-Nov-05 1:22 
QuestionNice Code, but why not just use JavaScript? Pin
Anonymous22-Jul-05 16:16
sussAnonymous22-Jul-05 16:16 
AnswerRe: Nice Code, but why not just use JavaScript? Pin
Sreejith Thathanattu22-Oct-06 23:54
memberSreejith Thathanattu22-Oct-06 23:54 
GeneralWriting to the Checkboxes Pin
BrentLak27-Jun-05 4:52
memberBrentLak27-Jun-05 4:52 
GeneralIt works Pin
ldaniel9-Jun-05 4:27
memberldaniel9-Jun-05 4:27 
Questioncan believe Pin
Akantos30-May-05 11:38
memberAkantos30-May-05 11:38 
GeneralPlease help to setup &quot;check all&quot; Pin
Anonymous21-Apr-05 3:59
sussAnonymous21-Apr-05 3:59 
GeneralRe: Please help to setup &quot;check all&quot; Pin
Noman Nadeem22-Apr-05 19:22
memberNoman Nadeem22-Apr-05 19:22 
GeneralRe: Please help to setup &quot;check all&quot; Pin
Michelle Tucker23-Apr-05 13:07
sussMichelle Tucker23-Apr-05 13:07 
GeneralRe: Please help to setup &quot;check all&quot; Pin
Noman Nadeem23-Apr-05 18:30
memberNoman Nadeem23-Apr-05 18:30 
GeneralRe: Please help to setup &quot;check all&quot; Pin
Casual Jim30-May-05 0:57
sussCasual Jim30-May-05 0:57 
GeneralRe: Please help to setup &quot;check all&quot; Pin
jachyuen6-Jul-05 16:30
memberjachyuen6-Jul-05 16:30 
GeneralRe: Please help to setup &quot;check all&quot; Pin
MiniDawg25-Jan-06 9:12
memberMiniDawg25-Jan-06 9:12 
GeneralError Pin
Sel_13-Apr-05 16:29
memberSel_13-Apr-05 16:29 
GeneralRe: Error Pin
Noman Nadeem13-Apr-05 20:19
memberNoman Nadeem13-Apr-05 20:19 
Generalserious errors any idea what you are doing Pin
Tereas preatur23-Apr-05 13:12
sussTereas preatur23-Apr-05 13:12 
GeneralRe: serious errors any idea what you are doing Pin
Noman Nadeem23-Apr-05 18:33
memberNoman Nadeem23-Apr-05 18:33 
GeneralMetabuilders row selector column Pin
Simon Capewell13-Apr-05 2:57
memberSimon Capewell13-Apr-05 2:57 
Questionsuport? Pin
tengtium12-Apr-05 23:39
membertengtium12-Apr-05 23:39 
AnswerRe: suport? Pin
Noman Nadeem13-Apr-05 6:45
memberNoman Nadeem13-Apr-05 6:45 
GeneralNot Bad... Pin
Bassam Abdul-Baki11-Apr-05 8:23
memberBassam Abdul-Baki11-Apr-05 8: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.150804.4 | Last Updated 11 Apr 2005
Article Copyright 2005 by Noman Nadeem
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid