Results 1 to 10 of 10

Thread: [RESOLVED] DataGridViews & checkboxes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Resolved [RESOLVED] DataGridViews & checkboxes

    Hi,

    I have a datagridview and in it a column of checkboxes. How can I get it to where IF ANY of the checkboxes in that column are clicked on (event change), then a MessageBox will show up...

    Thanks for the help!!!

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: DataGridViews & checkboxes

    I have something like:

    Code:
     Private Sub dgvWeldingLog_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvWeldingLog.CellContentClick
    
            MessageBox.Show("hello", "world", MessageBoxButtons.OK, MessageBoxIcon)
    
    End Sub
    This works, but it works for ALLL the columns. I only want it to do it for the column with the checkbox in it.
    Last edited by c3p0; Aug 24th, 2011 at 10:19 AM. Reason: new code & issue

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataGridViews & checkboxes

    If you look in your event signature you will see the variable 'e'. If you access the members of said variable you will see some properties that will help you.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridViews & checkboxes

    Replace YourColName with the column name to work with. I do not check to see the type of column as I am sure the name is correct in regards to YourColName is a CheckBox column type.

    Code:
    Private Sub DataGridView1_CellValueChanged( _ 
    	ByVal sender As Object, _ 
    	ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ 
    	Handles DataGridView1.CellValueChanged
    	
        If DataGridView1.CurrentRow IsNot Nothing Then
            If e.ColumnIndex = DataGridView1.Columns("YourColName").Index Then
                If CBool(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("YourColName").Value) Then
                    MsgBox("Checked")
                Else
                    MsgBox("UnChecked")
                End If
            End If
        End If
        
    End Sub

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridViews & checkboxes

    As per ForumnAccount

    Code:
    Private Sub DataGridView1_CellContentClick( _ 
    	ByVal sender As Object, _ 
    	ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ 
    	Handles DataGridView1.CellContentClick
    	
        If DataGridView1.CurrentRow IsNot Nothing Then
            If e.ColumnIndex = DataGridView1.Columns("YourColName").Index Then
                If CBool(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("YourColName").Value) Then
                    MsgBox("YourColName")
                Else
                    MsgBox("UnChecked")
                End If
            End If
        End If
    End Sub

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridViews & checkboxes

    Also here is a demo I did with what I call dualling checkbox columns
    http://www.vbforums.com/showpost.php...34&postcount=9

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: DataGridViews & checkboxes

    Thanks for the response! I finally figured it out before I even read your post. This seems to work...so far:

    Code:
    Private Sub dgvstuff_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvstuff.CellContentClick
    
            If dgvstuff.CurrentRow.Cells("dgvcheckboxcolumn").Selected = True Then
                MessageBox.Show("hello", "world", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            End If
    
        End Sub

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridViews & checkboxes

    Quote Originally Posted by c3p0 View Post
    Thanks for the response! I finally figured it out before I even read your post. This seems to work...so far:

    Code:
    Private Sub dgvstuff_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvstuff.CellContentClick
    
            If dgvstuff.CurrentRow.Cells("dgvcheckboxcolumn").Selected = True Then
                MessageBox.Show("hello", "world", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            End If
    
        End Sub
    Great although you should get in the habit to check that there is a current row, maybe there always is in this code but not in another project.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: DataGridViews & checkboxes

    Quote Originally Posted by kevininstructor View Post
    As per ForumnAccount

    Code:
    Private Sub DataGridView1_CellContentClick( _ 
    	ByVal sender As Object, _ 
    	ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ 
    	Handles DataGridView1.CellContentClick
    	
        If DataGridView1.CurrentRow IsNot Nothing Then
            If e.ColumnIndex = DataGridView1.Columns("YourColName").Index Then
                If CBool(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("YourColName").Value) Then
                    MsgBox("Checked")
                Else
                    MsgBox("UnChecked")
                End If
            End If
        End If
    End Sub
    Hey, Thanks for the help! I'm actually running across a minor issue with both of our codes. Whenever I check a checkbox that's empty, it still gives me the message "unchecked" instead of "checked". Actually it NEVER gives me the message "checked".

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: DataGridViews & checkboxes

    Here is a working copy where only the first CheckBox column is done with a Msgbox. This attached VS2008 is a slimmed down version of the one I suggested you review in an earlier post.
    Attached Files Attached Files

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