Results 1 to 6 of 6

Thread: [RESOLVED] Referencing a DataGridViewCheckBox?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Referencing a DataGridViewCheckBox?

    I have a DataGridView that has a column defined as a CheckBox. In the CellClick Event I want to be able to check to see if that CheckBox has been checked or not. I'm not sure how to check it. Any ideas?

    Thanks,
    Blake

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Referencing a DataGridViewCheckBox?

    The CellClick event is an unreliable event for what you're wanting to do, but this is how you'd do it:
    Code:
        Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If Not (IsNothing(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
                MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
            End If
        End Sub
    Here is how I'd do it using the CellValueChanged Event:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private isloading As Boolean = True
        Private Sub DataGridView1_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            If Not (isloading) AndAlso Not (IsNothing(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
                MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
            End If
        End Sub
    
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As System.Object, e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            If Not (IsNothing(DataGridView1.IsCurrentCellDirty)) AndAlso DataGridView1.IsCurrentCellDirty Then
                DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'The only way I could think to get past the form_load event.
            isloading = False
        End Sub
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Referencing a DataGridViewCheckBox?

    For a check of the whole column ...

    vb.net Code:
    1. For Each row As DataGridViewRow In DataGridView1.Rows
    2.             If CBool(CType(row.Cells(0), DataGridViewCheckBoxCell).Value) Then
    3.                 row.Cells(1).Value = "Yes"
    4.             Else
    5.                 row.Cells(1).Value = "No"
    6.             End If
    7.         Next

    ... or if you've specifically designated TrueValue and FalseValue for the column (here as "Yes" and "No"), simply ...

    vb.net Code:
    1. For Each row As DataGridViewRow In DataGridView1.Rows
    2.             row.Cells(1).Value = row.Cells(0).Value
    3.         Next
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Referencing a DataGridViewCheckBox?

    Dun, quick question, and as this pertains to my answer to the OP I hope the OP doesn't feel as though I'm high jacking the thread. Could you see of anyway to get past the form_load event without an InvalidOperationsException being thrown? The only way I could think of was to set up a Boolean flag like I did.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Referencing a DataGridViewCheckBox?

    If Me.Visible will do the trick. Just ensures that everything's initialised completely.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Referencing a DataGridViewCheckBox?

    dday,

    Your solution worked...thanks!
    Blake

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