Click here to Skip to main content
Licence 
First Posted 14 Dec 2002
Views 151,301
Downloads 2,876
Bookmarked 49 times

Add a Checkbox Control to the DataGrid Control

By cuepack | 14 Dec 2002
Add CheckBox to ASP.NET DataGrid control (VB.NET version).
5 votes, 35.7%
1
3 votes, 21.4%
2
1 vote, 7.1%
3
2 votes, 14.3%
4
3 votes, 21.4%
5
2.71/5 - 14 votes
μ 2.43, σa 2.91 [?]

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... 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  
This article was posted by cuepack to be a VB version of my C# article http://www.codeproject.com/aspnet/DataGridCheckBox.asp[^]. However the technique used here uses TemplateColumn rather than creating your own module derived from TemplateColumn and shows the other way of creating these sort of grid columns. The advantage of this technique is that it is easier and quicker to create a single one of these columns in this manner - the disadvantage is that it is less portable. Of course the examples given by both are merely examples of how to create your own template column within a DataGrid. I don't know about cuepack but I use both techniques depending on the situation. My most complex version that I use in class form has 4 editable controls in edit mode and is used in over 20 web screens. But most of the time I use the above method.
 
I think that cuepack should have mentioned what he had to do in the .aspx file as this is the part where most beginners fall down.
 
eg this is his code for the .aspx page
 
<asp:TemplateColumn HeaderText="Export">
    <HeaderStyle HorizontalAlign="Center" Width="2%"></HeaderStyle>
    <ItemTemplate>
        <asp:CheckBox ID="chkExport" Runat="server" />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:CheckBox ID="chkExportON" Runat="server" Enabled="true" />
    </EditItemTemplate>
</asp:TemplateColumn>
 
also cuepack hasn't described on how we would bind a boolean field from the database to this checkbox column and I think this is another area that could be expanded to enhance this article.
 
I hope that you find these comments constructive and look forward to your reply.
 

 

Technically speaking the dictionary would define Visual Basic users as programmers.
But here again, a very generalized, liberal definition is being employed and it's wrong
- just plain wrong - Tom Archer 5/12/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    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
Web03 | 2.5.120210.1 | Last Updated 15 Dec 2002
Article Copyright 2002 by cuepack
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid