Click here to Skip to main content
6,597,576 members and growing! (21,297 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, ASP.NET, Visual Studio, Dev
Posted:14 Dec 2002
Views:138,660
Bookmarked:48 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
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


Member

Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  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... PinmemberDestiny77713:27 22 May '08  
GeneralcmdFindSelected_Click PinmemberKathy 99997:47 1 Aug '05  
Generaldatagrid Pinsusspraveen saini6:42 7 Mar '05  
GeneralRe: datagrid PinmemberYetAnotherGr8Guy23:38 22 Nov '05  
Generalprogressbar datagrid PinsussAnonymous0:25 3 Jun '04  
GeneralClient Side Action for select All PinsussAkram Kayani21:44 18 Sep '03  
Generalcounterpoint PinmemberShaun Wilde11:35 16 Dec '02  
GeneralA couple suggestions PinmemberMarc Clifton14:52 15 Dec '02  
GeneralRe: A couple suggestions PinmemberDavid Stone20:03 15 Dec '02  
GeneralRe: A couple suggestions PinmemberMarc 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-2009
Web11 | Advertise on the Code Project