Click here to Skip to main content
13,291,254 members (56,551 online)
Click here to Skip to main content
Add your own
alternative version


165 bookmarked
Posted 11 Sep 2003

Multi-select ASP.NET datagrid

Rate this:
Please Sign up or sign in to vote.
How to extend ASP.NET datagrid for multi-selection of data rows.

Sample image


ASP.NET datagrid is a very powerful and flexible control, however some features like multi-select is not natively available. This article shows how easily this functionality can be achieved with a few simple tricks.


One of the projects I was working on had a User Inteface requirement for multi-selection of grid rows. User wanted Hotmail or Yahoo style multi-selection facility along with highlighting the selection (hard part).

Using the code

After testing and applying some javascript and grid related coding techinque, I came up with the following working solution.

  • Add Template column for CHECKBOXES for selection (Hotmail/Yahoo style)
  • Add client-side onclick() and javascript for checkboxes, to highlight and mark checked rows.
  • Add server side CheckedChanged() event for preserving highlights. [because everytime on postback datagrid resets colors for the selection]
   <asp:CheckBox id="chkAll" 

      onclick="javascript:SelectAllCheckboxes(this);" runat="server" 

    AutoPostBack="false" ToolTip="Select/Deselect All" />
    <asp:CheckBox id="chkSelect" onclick="javascript:HighlightRow(this);"

        runat="server"OnCheckedChanged= "grdEmployees_CheckedChanged" 

        AutoPostBack="false" />


This function is used to have Hotmail style selection, it iterate through every check box on the form and then selects/deselects the checkboxes.


The only challenge left was to highlight and un-highlight the rows on selection and deselection. For which, I used

function, please note one very important thing when using <asp:CheckBox> control. It surrounds <SPAN> tags around CHECKBOX and therfore in javascript you have to get the children of the <SPAN> tag.

// Select all the checkboxes (Hotmail style)
function SelectAllCheckboxes(spanChk){

// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox=oItem.item(0)

    if(elm[i].type=="checkbox" && elm[i].id!

//----Select highlish rows when the checkboxes are selected
// Note: The colors are hardcoded, however you can use
//       RegisterClientScript blocks methods to use Grid's
//       ItemTemplates and SelectTemplates colors.
//         for ex: grdEmployees.ItemStyle.BackColor OR
//                 grdEmployees.SelectedItemStyle.BackColor
function HighlightRow(chkB)    {
var oItem = chkB.children;
       // grdEmployees.SelectedItemStyle.BackColor'white';
       // grdEmployees.SelectedItemStyle.ForeColor
// -->

This was the client side story. So far so good. One may argue why not use the plain simple HTML checkbox control? The answer is ASP.NET server control has a viewstate and therefore posting a page retains the rows selection.

Server Side

Now, On the server side we have to make sure the highlights are intact, because on every postback ASP.NET renders grid and loses the highlights. Following method is used for re-rendering the highlights.

Public Sub grdEmployees_CheckedChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs)
  Dim chkTemp As CheckBox = CType(sender, CheckBox)

  Dim dgi As DataGridItem 

  dgi = CType(chkTemp.Parent.Parent, DataGridItem)
  If (chkTemp.Checked) Then 

    dgi.BackColor = grdEmployees.SelectedItemStyle.BackColor 
    dgi.ForeColor = grdEmployees.SelectedItemStyle.ForeColor

    dgi.BackColor = grdEmployees.ItemStyle.BackColor
    dgi.ForeColor = grdEmployees.ItemStyle.ForeColor 
  End If
End Sub

Getting your selection

Its easy! Iterate through the DataGridItems collection and grab the checkbox [for ex. DemoGridItem.Cells(0).Controls(1)]. And verify its CHECKED property. Oh, also you can use DataKeyField of the dataset to grab specific datarows. Check out the attached code and you will love to find out how easy it is to multi-select rows.


Follow the downloaded code, you can simply use any SQL database. This is my very first article, hope .NET lovers would like it. Feedback is welcome.


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

Prashant Nayak (.Net Lover)
Web Developer
United States United States
Cranking code more than 12 years. Technical/Project lead/MCSD. Offered services to various industuries like S/W, Telecom, Publishing, Insurance etc.

When not on computer, I play/swim/read with my kids and help my better half (of course my lovely wife) to clean house. Solving challenging S/W problems is my passion. Hate non-productive meetings. Do lots of GOOGLE and Eat/Drink/Sleep around MSDN.

You may also be interested in...

Comments and Discussions

GeneralCommand button in data Grid Pin
S.Das2-Mar-06 21:28
memberS.Das2-Mar-06 21:28 
GeneralJavascript Functions Pin
Shadi Rahmani4-Sep-05 22:40
memberShadi Rahmani4-Sep-05 22:40 
GeneralRe: Javascript Functions Pin
enjoycrack5-Sep-05 1:12
memberenjoycrack5-Sep-05 1:12 
AnswerRe: Javascript Functions Pin
Sasi Atia12-Feb-06 21:52
memberSasi Atia12-Feb-06 21:52 
GeneralRe: Javascript Functions Pin
hb3219-Mar-08 4:47
memberhb3219-Mar-08 4:47 
Generalc# version Pin
Loic Deniel23-Aug-05 23:16
memberLoic Deniel23-Aug-05 23:16 
GeneralRe: c# version Pin
Anonymous26-Oct-05 9:04
sussAnonymous26-Oct-05 9:04 
Questionhow to get cell Text when AutoGenerateColumns="false" Pin
hostlike4-Aug-05 20:57
memberhostlike4-Aug-05 20:57 
GeneralModified client script Pin
dpadevet21-Jul-05 4:26
memberdpadevet21-Jul-05 4:26 
GeneralRe: Modified client script Pin
htb huang19-Mar-07 13:32
memberhtb huang19-Mar-07 13:32 
GeneralButton instead of CheckBox Pin
Jureshka7-Jul-05 5:53
memberJureshka7-Jul-05 5:53 
GeneralMozilla Firefox Browers Pin
taithien23-Jun-05 0:55
membertaithien23-Jun-05 0:55 
GeneralRe: Mozilla Firefox Browers Pin
wildwildwolf20-Jul-05 10:50
memberwildwildwolf20-Jul-05 10:50 
Generalscript correction Pin
][Turpa28-Mar-05 1:51
member][Turpa28-Mar-05 1:51 
General, Pin
Hui Yee4-Mar-05 17:34
memberHui Yee4-Mar-05 17:34 
Generalhope! Pin
tengtium3-Mar-05 3:56
membertengtium3-Mar-05 3:56 
GeneralRe: hope! Pin
zedaspintas6-Apr-05 0:35
memberzedaspintas6-Apr-05 0:35 
GeneralDude, this is been done before and better on DotNetJunkies. Be original! Pin
Anonymous9-Jan-05 7:13
sussAnonymous9-Jan-05 7:13 
GeneralRe: Dude, this is been done before and better on DotNetJunkies. Be original! Pin
Anonymous10-Jan-05 4:22
sussAnonymous10-Jan-05 4:22 
GeneralRe: Dude, this is been done before and better on DotNetJunkies. Be original! Pin
Anonymous31-Aug-05 3:30
sussAnonymous31-Aug-05 3:30 
GeneralRe: Dude, this is been done before and better on DotNetJunkies. Be original! Pin
Chetan Sharma14-Aug-06 12:51
memberChetan Sharma14-Aug-06 12:51 
GeneralRe: Dude, this is been done before and better on DotNetJunkies. Be original! Pin
Mikel Dublin24-Sep-07 1:16
memberMikel Dublin24-Sep-07 1:16 
Generalnothing Pin
Anonymous10-Dec-04 5:16
sussAnonymous10-Dec-04 5:16 
GeneralDatagrid Check all function problem Pin
shiva kumar5828-Nov-04 23:27
sussshiva kumar5828-Nov-04 23:27 
GeneralDatagrid Check all function problem Pin
shiva kumar5828-Nov-04 23:27
membershiva kumar5828-Nov-04 23:27 
GeneralDoesn't work Pin
cah_batak11-Nov-04 22:38
membercah_batak11-Nov-04 22:38 
GeneralTHX for sharing Pin
esman21-Nov-04 14:50
memberesman21-Nov-04 14:50 
GeneralSome little Bugfixings Pin
Grischa Brockhaus25-Oct-04 7:21
sussGrischa Brockhaus25-Oct-04 7:21 
GeneralRe: Some little Bugfixings Pin
shakespeare218919-Dec-04 20:15
membershakespeare218919-Dec-04 20:15 
Generalform.element problem Pin
MrKhalid11-Sep-04 10:39
memberMrKhalid11-Sep-04 10:39 
GeneralData Grid Pin
yathiraju_n9-Sep-04 20:39
memberyathiraju_n9-Sep-04 20:39 
GeneralRe: Data Grid Pin
rizingstar7730-Nov-05 12:24
memberrizingstar7730-Nov-05 12:24 
GeneralYea, it works...i did it Pin
K.Nagalinga Reddy29-Aug-04 6:38
memberK.Nagalinga Reddy29-Aug-04 6:38 
GeneralRe: Yea, it works...i did it Pin
Anonymous6-May-05 10:35
sussAnonymous6-May-05 10:35 
GeneralJust Great but I Would Like one Select.. Pin
Manolo Herrera18-Aug-04 8:51
memberManolo Herrera18-Aug-04 8:51 
GeneralCheckedChanged event does not fire Pin
Bharat Gadhia18-Aug-04 7:49
memberBharat Gadhia18-Aug-04 7:49 
GeneralRe: CheckedChanged event does not fire Pin
wildwildwolf19-Jul-05 13:16
memberwildwildwolf19-Jul-05 13:16 
GeneralLoad form with check boxes populated Pin
Anonymous13-Aug-04 14:55
sussAnonymous13-Aug-04 14:55 
GeneralRe: Load form with check boxes populated Pin
Prashant Nayak (.Net Lover)16-Aug-04 4:15
memberPrashant Nayak (.Net Lover)16-Aug-04 4:15 
GeneralSome column not change Pin
Q_Quek8-Aug-04 19:52
memberQ_Quek8-Aug-04 19:52 
GeneralRe: Some column not change Pin
Prashant Nayak (.Net Lover)9-Aug-04 6:11
memberPrashant Nayak (.Net Lover)9-Aug-04 6:11 
GeneralRe: Some column not change Pin
Q_Quek9-Aug-04 16:30
memberQ_Quek9-Aug-04 16:30 
GeneralError in javascript : object not found Pin
chinb5-Aug-04 6:11
memberchinb5-Aug-04 6:11 
QuestionHow To Print data in datagrid Pin
tidigirs1-Aug-04 22:04
membertidigirs1-Aug-04 22:04 
GeneralI gave it a '4'. Pin
Ashaman16-Jul-04 3:38
memberAshaman16-Jul-04 3:38 
GeneralRe: I gave it a '4'. Pin
Prashant Nayak (.Net Lover)16-Jul-04 4:36
memberPrashant Nayak (.Net Lover)16-Jul-04 4:36 
GeneralRe: I gave it a '4'. Pin
Jan Vercauteren5-Aug-04 4:51
memberJan Vercauteren5-Aug-04 4:51 
GeneralRe: I gave it a '4'. Pin
Ashaman5-Aug-04 6:01
memberAshaman5-Aug-04 6:01 
Generalonly 1 form allowed on page Pin
Anonymous12-Jul-04 15:24
sussAnonymous12-Jul-04 15:24 
GeneralRe: only 1 form allowed on page Pin
AutoHonky13-Jul-04 2:32
memberAutoHonky13-Jul-04 2:32 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 8 Mar 2004
Article Copyright 2003 by Prashant Nayak (.Net Lover)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid