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

DataGrid CheckBox Select Multiple Rows Column

By , 11 Apr 2005
 
Prize winner in Competition "ASP.NET Mar 2005"

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)

About the Author

Noman Nadeem
Web Developer
Saudi Arabia Saudi Arabia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhow to select through checkboxes?memberMember 466308727 Apr '08 - 20:57 
QuestionHow Select a row in Data Gridmemberpatelmurtuza16 Mar '07 - 2:14 
Questionhow can i tell what my project/library assembly ismemberinsignificant15 Mar '07 - 6:30 
GeneralIt is not a good ideamemberMember #2738631 Jan '07 - 21:56 
QuestionSpecified cast is not validmemberkrimsondon19 Dec '06 - 9:49 
QuestionHow to fetch info abt checked rowsmembermontysehra19 Nov '06 - 23:35 
QuestionCheck All button Does not workmemberStephen Noronha20 Oct '06 - 8:55 
AnswerRe: Check All button Does not workmemberphantomvie29 Nov '07 - 23:21 
GeneralRe: Check All button Does not workmemberMember 279619825 Jan '10 - 1:54 
Generalmy problem:my datasource is a arrayList [modified]memberrengdongzwf11 Jun '06 - 20:28 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 11 Apr 2005
Article Copyright 2005 by Noman Nadeem
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid