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

Add a Checkbox Control to the DataGrid Control

Rate me:
Please Sign up or sign in to vote.
2.80/5 (14 votes)
14 Dec 2002 176K   3.4K   49   10
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 + / -.

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

VB
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


Written By
Software Developer (Senior)
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

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
Destiny77722-May-08 12:27 
GeneralcmdFindSelected_Click Pin
Kathy 99991-Aug-05 6:47
Kathy 99991-Aug-05 6:47 
Generaldatagrid Pin
praveen saini7-Mar-05 5:42
praveen saini7-Mar-05 5:42 
GeneralRe: datagrid Pin
YetAnotherGr8Guy22-Nov-05 22:38
YetAnotherGr8Guy22-Nov-05 22:38 
Generalprogressbar datagrid Pin
Anonymous2-Jun-04 23:25
Anonymous2-Jun-04 23:25 
I want add a progressbar to the datagrid, but I have a xml document with the values
GeneralClient Side Action for select All Pin
akram_kayani18-Sep-03 20:44
akram_kayani18-Sep-03 20:44 
Generalcounterpoint Pin
Shaun Wilde16-Dec-02 10:35
Shaun Wilde16-Dec-02 10:35 
GeneralA couple suggestions Pin
Marc Clifton15-Dec-02 13:52
mvaMarc Clifton15-Dec-02 13:52 
GeneralRe: A couple suggestions Pin
David Stone15-Dec-02 19:03
sitebuilderDavid Stone15-Dec-02 19:03 
GeneralRe: A couple suggestions Pin
Marc Clifton16-Dec-02 0:32
mvaMarc Clifton16-Dec-02 0: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.