Results 1 to 3 of 3

Thread: Check all boxes in a datagridview (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Check all boxes in a datagridview (Resolved)

    I am trying to check all the text boxes in a datagrid view with this
    VB Code:
    1. Dim ctl As Control
    2.         Dim cbx As CheckBox
    3.         For Each ctl In Me.dgvPosiPay.Controls
    4.             If ctl.GetType Is cbx.GetType Then
    5.                 ctl = DirectCast(ctl, CheckBox)
    6.                 If ctl.Checked = False Then
    7.                     ctl.checked = True
    8.                 End If
    9.             End If
    10.         Next
    I am getting the error 'checked' is not a member of 'System.Windows.Forms.Control'. and it is underlining the ctl.checked in the second if statement. I am pretty sure my problem is with the Me.dgvPosiPay.Controls part of the code block but I am not sure what the correct syntax should be.
    Last edited by FastEddie; Aug 12th, 2008 at 09:37 AM.

  2. #2
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: Check all boxes in a datagridview

    VB Code:
    1. For Each ctl As Windows.Forms.Control In Me.dgvPosiPay.Controls
    2.             If TypeOf ctl Is CheckBox Then _
    3.                 DirectCast(ctl, CheckBox).Checked = True
    4.         Next
    Although, I'm pretty sure that this won't catch them. You're going to have to iterate through your datagridviewcells to find them.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Check all boxes in a datagridview

    This may not be the most elegant way to accomplish my goal but it worked so I am posting it so that others may use it.
    VB Code:
    1. Private Sub CheckAll()
    2.         Dim j As Integer
    3.         Try
    4.             For j = 0 To ds.Tables(0).Rows.Count - 1
    5.                 dgvPosiPay.Item(6, j).Value = CheckState.Checked
    6.             Next
    7.         Catch ex As Exception
    8.             Throw New Exception(ex.Message)
    9.         End Try
    10.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width