Click here to Skip to main content
15,887,746 members
Articles / Web Development / ASP.NET
Article

DataGrid CheckBox Select Multiple Rows Column

Rate me:
Please Sign up or sign in to vote.
4.80/5 (31 votes)
11 Apr 2005CPOL2 min read 254.5K   3.1K   85   46
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:

ASP.NET
<%@ 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.NET
<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:

C#
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:

C#
// 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)


Written By
Web Developer
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to select through checkboxes? Pin
Member 466308727-Apr-08 20:57
Member 466308727-Apr-08 20:57 
QuestionHow Select a row in Data Grid Pin
Murtuza Husain Miyan Patel16-Mar-07 2:14
professionalMurtuza Husain Miyan Patel16-Mar-07 2:14 
Questionhow can i tell what my project/library assembly is Pin
insignificant15-Mar-07 6:30
insignificant15-Mar-07 6:30 
GeneralIt is not a good idea Pin
Member 2738631-Jan-07 21:56
Member 2738631-Jan-07 21:56 
QuestionSpecified cast is not valid Pin
krimsondon19-Dec-06 9:49
krimsondon19-Dec-06 9:49 
QuestionHow to fetch info abt checked rows Pin
montysehra19-Nov-06 23:35
montysehra19-Nov-06 23:35 
QuestionCheck All button Does not work Pin
Stephen Noronha20-Oct-06 8:55
Stephen Noronha20-Oct-06 8:55 
AnswerRe: Check All button Does not work Pin
phantomvie29-Nov-07 23:21
phantomvie29-Nov-07 23:21 
GeneralRe: Check All button Does not work Pin
Member 279619825-Jan-10 1:54
Member 279619825-Jan-10 1:54 
Generalmy problem:my datasource is a arrayList [modified] Pin
rengdongzwf11-Jun-06 20:28
rengdongzwf11-Jun-06 20:28 
GeneralPaging Problem Pin
mshariq10-Jun-06 18:07
mshariq10-Jun-06 18:07 
GeneralTo check the checkbox Pin
huguesgauthier17-May-06 8:43
huguesgauthier17-May-06 8:43 
GeneralRe: To check the checkbox Pin
Ted Husted30-May-06 8:50
Ted Husted30-May-06 8:50 
GeneralRe: To check the checkbox Pin
Donald Bittenbender17-Dec-08 11:23
Donald Bittenbender17-Dec-08 11:23 
Generalcollecting information from a datagrid Pin
JustmeNick30-Apr-06 7:15
JustmeNick30-Apr-06 7:15 
GeneralCheck All isn't working Pin
BSegE12-Mar-06 5:54
BSegE12-Mar-06 5:54 
Hello Everyone,

As I understand it, when I implement this control I should see a checkbox column in my datagrid and when I check or uncheck the header checkbox, all of the individual checkboxes in the column should automatically be set accordingly. And, this is accomplished on the client using JavaScript.

My web application is being developed using VB.Net in an ASP.Net 1.1 environment.

I have added a new C# library project to my solution, successfully constructed a DgCheckboxColumn library dll and referenced it in my project.

I registered the control by adding the following line to the top of my HTML page:

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

And I added the control to my datagrid as the 1st column using the provided example.

Next I added the following line immediately after calling the databiind methid in my datagrid:

DGCheckBoxColumn.RegisterClientCheckEvents(Me, "WebForm1")

The good news is that everything builds correctly and when executed my page displays the new checkbox column correctly.

However when I check the box in the column header, nothing else changes and I receive “An error has occurred in the script on this page” error further indicating “‘length’ is a Null or not an object”.

I went back and stepped through the execution. When the dgcheckboxcolumn code executes, it creates the following two scripts:

<script language=JavaScript> function CheckAll( checkAllBox )<br />
 {<br />
  var frm = document.[frmID];<br />
  var ChkState=checkAllBox.checked;<br />
  for(i=0;i< frm.length;i++)<br />
  {<br />
         e=frm.elements[i];<br />
        if(e.type=='checkbox' && e.name.indexOf('checkboxCol') != -1)<br />
            e.checked= ChkState ;<br />
  }<br />
 }<br />
 </script><br />
<br />
<br />
 <script language=JavaScript>function CheckChanged()<br />
 {<br />
  var frm = document.[frmID];<br />
  var boolAllChecked;<br />
  boolAllChecked=true;<br />
  for(i=0;i< frm.length;i++)<br />
  {    e=frm.elements[i];<br />
  if ( e.type=='checkbox' && e.name.indexOf('checkboxCol') != -1 )<br />
      if(e.checked== false)<br />
      {<br />
         boolAllChecked=false;<br />
         break;<br />
      }<br />
  }<br />
  for(i=0;i< frm.length;i++)<br />
  {    e=frm.elements[i];<br />
    if ( e.type=='checkbox' && e.name.indexOf('checkboxHead') != -1 )<br />
    {<br />
      if( boolAllChecked==false)<br />
         e.checked= false ;<br />
      else<br />
         e.checked= true;<br />
      break;<br />
    }<br />
   }<br />
 }<br />
 </script>


I’m not Java literate, so I really can’t see anything obviously wrong here.

After building the scripts dgcheckboxcolumn moves on and ultimately fails in the registerattributes subroutine when executing the line:

CheckBox chk = (CheckBox)wc;

Ok, what did I do wrong? Sigh | :sigh:
NewsProjectAssembly Cannot Load Pin
torome10-Mar-06 16:51
torome10-Mar-06 16:51 
GeneralAdding data Pin
MiniDawg25-Jan-06 5:45
MiniDawg25-Jan-06 5:45 
GeneraldgchkbxCol.SelectedIndexes return empty Pin
layoro5-Jan-06 16:15
layoro5-Jan-06 16:15 
GeneralRe: dgchkbxCol.SelectedIndexes return empty Pin
MiniDawg26-Jan-06 7:14
MiniDawg26-Jan-06 7:14 
JokeRe: dgchkbxCol.SelectedIndexes return empty Pin
layoro26-Jan-06 14:25
layoro26-Jan-06 14:25 
JokeRe: dgchkbxCol.SelectedIndexes return empty Pin
MiniDawg27-Jan-06 2:50
MiniDawg27-Jan-06 2:50 
GeneralPage Errors...About Length Pin
ekasal1-Dec-05 20:48
ekasal1-Dec-05 20:48 
GeneralClick the header checkbox that is not select all checkbox Pin
Vincent Tan E H24-Aug-05 0:19
Vincent Tan E H24-Aug-05 0:19 
GeneralRe: Click the header checkbox that is not select all checkbox Pin
erikwi26-Nov-05 1:08
erikwi26-Nov-05 1:08 

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

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