Check all boxes in a datagridview (Resolved)
I am trying to check all the text boxes in a datagrid view with this
VB Code:
Dim ctl As Control
Dim cbx As CheckBox
For Each ctl In Me.dgvPosiPay.Controls
If ctl.GetType Is cbx.GetType Then
ctl = DirectCast(ctl, CheckBox)
If ctl.Checked = False Then
ctl.checked = True
End If
End If
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.
Re: Check all boxes in a datagridview
VB Code:
For Each ctl As Windows.Forms.Control In Me.dgvPosiPay.Controls
If TypeOf ctl Is CheckBox Then _
DirectCast(ctl, CheckBox).Checked = True
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.
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:
Private Sub CheckAll()
Dim j As Integer
Try
For j = 0 To ds.Tables(0).Rows.Count - 1
dgvPosiPay.Item(6, j).Value = CheckState.Checked
Next
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub