2011-05-15 178 views
1

我有一些用vb.net編寫的DataGridView代碼。 (datasource附件中沒有內容。如何檢測checkBox是否已選中或未選中?讀取單元格的值

此代碼奇怪地在隨機時間報告TRUE或FALSE。它甚至變成ONcheckbox比該行其他行我點擊。

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 

所有我在這裏看到(和其他國家)的其他例子似乎並沒有幫助。他們的解決方案始終是:

  • 只需檢查單元格的VALUE。
  • 只要學習c#,並學會將其轉換爲vb.net。
  • 只需檢查VALUE爲空,或爲空或「」或全部。
  • 將VALUE轉換爲bool。
  • 改爲將它附加到數據源。
  • 設置TrueValue和FalseValue。

我已經嘗試了無數其他方法,似乎沒有一個在vb.net中實際獲得checkbox ON/OFF值。

回答

0

演員單元格的值,以布爾:

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