Click here to Skip to main content
11,647,748 members (67,753 online)
Click here to Skip to main content

Add a Checkbox Control to the DataGrid Control

, 14 Dec 2002 161.9K 3.3K 49
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

cuepack
Software Developer (Senior)
Switzerland Switzerland
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThanks for a clear example how to do this using VB.Net for an ASPX Web Page... Pin
Destiny77722-May-08 12:27
memberDestiny77722-May-08 12:27 
GeneralcmdFindSelected_Click Pin
Kathy 99991-Aug-05 6:47
memberKathy 99991-Aug-05 6:47 
Generaldatagrid Pin
praveen saini7-Mar-05 5:42
susspraveen saini7-Mar-05 5:42 
GeneralRe: datagrid Pin
YetAnotherGr8Guy22-Nov-05 22:38
memberYetAnotherGr8Guy22-Nov-05 22:38 
Generalprogressbar datagrid Pin
Anonymous2-Jun-04 23:25
sussAnonymous2-Jun-04 23:25 
GeneralClient Side Action for select All Pin
Akram Kayani18-Sep-03 20:44
sussAkram Kayani18-Sep-03 20:44 
Generalcounterpoint Pin
Shaun Wilde16-Dec-02 10:35
memberShaun Wilde16-Dec-02 10:35 
GeneralA couple suggestions Pin
Marc Clifton15-Dec-02 13:52
memberMarc Clifton15-Dec-02 13:52 
GeneralRe: A couple suggestions Pin
David Stone15-Dec-02 19:03
memberDavid Stone15-Dec-02 19:03 
GeneralRe: A couple suggestions Pin
Marc Clifton16-Dec-02 0:32
memberMarc Clifton16-Dec-02 0:32 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150804.3 | Last Updated 15 Dec 2002
Article Copyright 2002 by cuepack
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid