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