Click here to Skip to main content
Licence 
First Posted 14 Dec 2002
Views 152,862
Bookmarked 49 times

Add a Checkbox Control to the DataGrid Control

By | 14 Dec 2002 | Article
Add CheckBox to ASP.NET DataGrid control (VB.NET version).

Sample Image - checkBoxGrid.jpg

Introduction

This simple demo project shows, how you can add CheckBoxes to the DataGrid web control. This article shows handling automatic selection and getting the selected CheckBoxes, too.

Select all / Deselect all items

This simple method iterates through the grid and sets the Checked state of the CheckBoxes in the grid. And sets the Button's Caption to + / -.

 Private Sub selectAll()
        Dim oDataGridItem As DataGridItem
        Dim chkExport As System.Web.UI.WebControls.CheckBox

        If cmdSelectAll.Text = "+" Then
            For Each oDataGridItem In dgMain.Items

                chkExport = oDataGridItem.FindControl("chkExport")
                chkExport.Checked = True
            Next

            cmdSelectAll.Text = "-"
        Else
            For Each oDataGridItem In dgMain.Items
                chkExport = oDataGridItem.FindControl("chkExport")
                chkExport.Checked = False
            Next
            cmdSelectAll.Text = "+"
        End If
    End Sub

Find selected Items

This method iterate through the grid items, and adds the selected rows to an array.

    Private Sub cmdFindSelected_Click(ByVal sender As _
                       System.Object, ByVal e As System.EventArgs) _
                       Handles cmdFindSelected.Click

        Dim oDataGridItem As DataGridItem
        Dim chkExport As System.Web.UI.WebControls.CheckBox
        Dim oExArgs As New System.Collections.ArrayList()
        Dim sID As String
        
        For Each oDataGridItem In dgMain.Items

            chkExport = oDataGridItem.FindControl("chkExport")
            If chkExport.Checked Then
                sID = _
                  CType(oDataGridItem.FindControl("lblColumn"), Label).Text
                oExArgs.Add(sID)
            End If
        Next

    End Sub

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

cuepack

Software Developer (Senior)

Switzerland Switzerland

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks for a clear example how to do this using VB.Net for an ASPX Web Page... PinmemberDestiny77712:27 22 May '08  
GeneralcmdFindSelected_Click PinmemberKathy 99996:47 1 Aug '05  
Generaldatagrid Pinsusspraveen saini5:42 7 Mar '05  
asp.net using C#
 
i want 2 use checkbox on the datagrid, 4 that i written code bt unable click event not responding 2 to click procedure of the datagrid,
 
pl.let me code 4 this

GeneralRe: datagrid PinmemberYetAnotherGr8Guy22:38 22 Nov '05  
Generalprogressbar datagrid PinsussAnonymous23:25 2 Jun '04  
GeneralClient Side Action for select All PinsussAkram Kayani20:44 18 Sep '03  
Generalcounterpoint PinmemberShaun Wilde10:35 16 Dec '02  
GeneralA couple suggestions PinmemberMarc Clifton13:52 15 Dec '02  
GeneralRe: A couple suggestions PinmemberDavid Stone19:03 15 Dec '02  
GeneralRe: A couple suggestions PinmemberMarc Clifton0:32 16 Dec '02  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 15 Dec 2002
Article Copyright 2002 by cuepack
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid