5,696,038 members and growing! (10,394 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate

DataGrid CheckBox Select Multiple Rows Column

By Noman Nadeem

Allow users to select multiple records from your DataGrid. "CheckAll / UnCheck All" functionality on client side.
C#, Windows, .NET 1.1, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 11 Apr 2005
Updated: 11 Apr 2005
Views: 129,267
Bookmarked: 66 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
Prize winner in Competition "ASP.NET Mar 2005"
29 votes for this Article.
Popularity: 6.70 Rating: 4.58 out of 5
0 votes, 0.0%
1
1 vote, 3.4%
2
2 votes, 6.9%
3
5 votes, 17.2%
4
21 votes, 72.4%
5

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

Noman Nadeem



Occupation: Web Developer
Location: Saudi Arabia Saudi Arabia

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
Generalhow to select through checkboxes?memberMember 466308721:57 27 Apr '08  
GeneralHow Select a row in Data Gridmemberpatelmurtuza3:14 16 Mar '07  
Questionhow can i tell what my project/library assembly ismemberinsignificant7:30 15 Mar '07  
GeneralIt is not a good ideamember22:56 31 Jan '07  
QuestionSpecified cast is not validmemberkrimsondon10:49 19 Dec '06  
GeneralHow to fetch info abt checked rowsmembermontysehra0:35 20 Nov '06  
QuestionCheck All button Does not workmemberStephen Noronha9:55 20 Oct '06  
AnswerRe: Check All button Does not workmemberphantomvie0:21 30 Nov '07  
Generalmy problem:my datasource is a arrayList [modified]memberrengdongzwf21:28 11 Jun '06  
GeneralPaging Problemmembermshariq19:07 10 Jun '06  
GeneralTo check the checkboxmemberOlog-hai9:43 17 May '06  
GeneralRe: To check the checkboxmemberTed Husted9:50 30 May '06  
Generalcollecting information from a datagridmemberJustmeNick8:15 30 Apr '06  
GeneralCheck All isn't workingmemberBSegE6:54 12 Mar '06  
NewsProjectAssembly Cannot Loadmembertorome17:51 10 Mar '06  
GeneralAdding datamemberMiniDawg6:45 25 Jan '06  
GeneraldgchkbxCol.SelectedIndexes return emptymemberlayoro17:15 5 Jan '06  
GeneralRe: dgchkbxCol.SelectedIndexes return emptymemberMiniDawg8:14 26 Jan '06  
JokeRe: dgchkbxCol.SelectedIndexes return emptymemberlayoro15:25 26 Jan '06  
JokeRe: dgchkbxCol.SelectedIndexes return emptymemberMiniDawg3:50 27 Jan '06  
GeneralPage Errors...About Lengthmemberekasal21:48 1 Dec '05  
GeneralClick the header checkbox that is not select all checkboxmemberVincent Tan E H1:19 24 Aug '05  
GeneralRe: Click the header checkbox that is not select all checkboxmembererikwi2:08 26 Nov '05  
GeneralRe: Click the header checkbox that is not select all checkboxmembererikwi2:22 26 Nov '05  
GeneralNice Code, but why not just use JavaScript?sussAnonymous17:16 22 Jul '05