2010-08-09 38 views
0

我有一個datagridview(未綁定)。字段是姓名,家庭姓名和電話號碼以及複選框。在VB.Net中捕獲DataGridView複選框的值

該DataGridView中有10行。

有一個OK按鈕

我需要得到哪些行用戶已籤顯示的消息。用戶單擊確定按鈕時應顯示消息。可能有幾條消息,逐一檢查每一行,循環。

我無法獲得此消息。我在OK按鈕中嘗試了以下代碼:

Dim strCB As String = dgvChooseQs.Rows(0).Cells(3).Value.ToString 

單元格(3)是我的複選框。不要考慮行(0),目前我只是檢查第0行的值。

感謝您的幫助。

Furqan

回答

0

你需要做這樣的事情:

if ctype(dgvChooseQs.Rows(0).findcontrol("whateverYourCheckBoxIsNamed"), checkbox).checked then 

'throw the message 

end if 
2

不要使用細胞指數。您的複選框列必須有一個名稱,以便您使用它。

否則,你想要做什麼會是這樣的

 

For each oRow as DataGridViewRow in dgvChooseQs.Rows 

    If oRow.Cells("ColNamE").Value = True then 
    'do whatever you need to do. 
    End if 

Next 

如果你覺得你需要轉換列,那麼你可以使用CTYPE,但類型是DataGridViewCheckBoxCell,不復選框。

1

您可以將單元格的值轉換爲布爾,然後檢查,如下所示:

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 
0

我發現了一個簡單的解決方案。

只需在單擊單元格後更改單元格焦點。

Private Sub DGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellContentClick 
    If e.ColumnIndex = "Here checkbox column id or name" Then 
     DGV.Item(e.ColumnIndex, e.RowIndex + 1).Selected = True 
     'Here your code 
     MsgBox DGV.Item(e.ColumnIndex, e.RowIndex).Value 
    End If 
End Sub 

不要忘記檢查您的(ckeckbox + 1)索引列是否存在。

0

將dataGridView中的網格類型設置爲'DataGridViewCheckBoxXColumn'而不是DataGridViewCheckBoxColumn。

所有問題都將解決