Results 1 to 4 of 4

Thread: [RESOLVED] Get Checkbox value from DataGridView - VB.net

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Resolved [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?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    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!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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...

Tags for this Thread

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