Reading the value of the cell
I have some DataGridView
code written in vb.net. (Nothing is attached to a datasource
.)
The 4th column is a checkboxCell
. How do I detect if that checkBox
is checked or unchecked?
This code strangely reports TRUE开发者_运维百科 or FALSE at random times. It even turns ON the checkbox
in rows other than the row I clicked in.
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim whichGrid As DataGridView = CType(sender, DataGridView)
Dim rowClicked As Int16 = e.RowIndex
Call MsgBox(rowClicked & vbCrLf & whichGrid.Rows(rowClicked).Cells(4).Value)
End Sub
All the other examples I've looked at here (and elsewhere) don't seem to help. Their solutions are always:
- Just check the cell's VALUE.
- Just learn c#, and learn to convert it to vb.net.
- Just check VALUE for nothing, or null, or "", or all of those.
- Convert VALUE to a bool.
- Attach it to a datasource instead.
- Set TrueValue and FalseValue.
I've tried countless other methods, none seem to actually get the checkbox
ON/OFF value in vb.net.
Cast the cell's value to a Boolean:
Dim RowIndex As Integer = ...
Dim ColumnIndex As Integer = ...
Dim IsTicked As Boolean = CBool(DataGridView1.Rows(RowIndex).Cells(ColumnIndex).Value)
If IsTicked Then
MessageBox.Show("You ticked the box.")
Else
MessageBox.Show("You cleared the box.")
End If
精彩评论