[RESOLVED] Get Checkbox value from DataGridView - VB.net
Trying to capture the value of a checkbox from a DataGridView. I've tried this a couple of ways but I get System.InvalidCastException. I'm not sure how else to cast this:
Code:
For i As Integer = 0 To TheGrid.Rows.Count - 1
If CBool(TheGrid.Rows(i).Cells("CheckOut").Value) = True Then
'do stuff
End If
Next
For i As Integer = 0 To TheGrid.Rows.Count - 1
Dim ChkState As Boolean = TheGrid.Rows(i).Cells("CheckOut").Value
If ChkState = True Then
'do stuff
End If
Next
Is there a specific way to cast as a DataGridViewCheckBoxCell?
Re: Get Checkbox value from DataGridView - VB.net
try this:
Code:
For i As Integer = 0 To TheGrid.Rows.Count - 1
If CBool(DirectCast(TheGrid.Rows(i).Cells("CheckOut"), DataGridViewCheckBoxCell).Value) = True Then
'do stuff
End If
Next
Re: Get Checkbox value from DataGridView - VB.net
Perfect!
Thank you, I have not used DirectCast before. I guess it acts like a forced cast? I'll read up on it. Thanks again!
Re: [RESOLVED] Get Checkbox value from DataGridView - VB.net
You'd use DirectCast when you know the underlying type of an Object. If you're not sure of the type you could use TryCast...