5,696,038 members and growing! (10,695 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

Add a Checkbox Control to the DataGrid Control

By cuepack

Add CheckBox to ASP.NET DataGrid control (VB.NET version).
Windows, .NET 1.0, .NET, ASP.NET, Visual Studio, Dev

Posted: 14 Dec 2002
Updated: 14 Dec 2002
Views: 128,832
Bookmarked: 42 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 2.62 Rating: 2.35 out of 5
5 votes, 38.5%
1
3 votes, 23.1%
2
1 vote, 7.7%
3
1 vote, 7.7%
4
3 votes, 23.1%
5

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



Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralThanks for a clear example how to do this using VB.Net for an ASPX Web Page...memberDestiny77713:27 22 May '08  
GeneralcmdFindSelected_ClickmemberKathy 99997:47 1 Aug '05  
Generaldatagridsusspraveen saini6:42 7 Mar '05  
GeneralRe: datagridmemberYetAnotherGr8Guy23:38 22 Nov '05  
Generalprogressbar datagridsussAnonymous0:25 3 Jun '04  
GeneralClient Side Action for select AllsussAkram Kayani21:44 18 Sep '03  
GeneralcounterpointmemberShaun Wilde11:35 16 Dec '02  
GeneralA couple suggestionsmemberMarc Clifton14:52 15 Dec '02  
GeneralRe: A couple suggestionsmemberDavid Stone20:03 15 Dec '02  
GeneralRe: A couple suggestionsmemberMarc Clifton1:32 16 Dec '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Dec 2002
Editor: Smitha Vijayan
Copyright 2002 by cuepack
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project