簡體   English   中英

在 VB.Net 中捕獲 DataGridView CheckBox 的值

[英]Capturing value of DataGridView CheckBox in VB.Net

我有一個數據網格視圖(未綁定)。 字段是姓名、姓氏和電話號碼以及復選框列。

該 DataGridView 中有十行。

有一個確定按鈕

我需要得到顯示用戶檢查了哪些行的消息。 當用戶單擊“確定”按鈕時,應該會出現該消息。 可能有幾條消息,在一個循環中逐行檢查。

我無法收到此消息。 我在確定按鈕中嘗試了以下代碼:

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

Cell(3) 是我的復選框。 不要考慮行(0),目前我只是檢查第 0 行的值

謝謝你的幫助。

富爾坎

不要使用單元格索引。 您的復選框列必須有一個名稱,因此您應該使用它。

否則,你想要做的是這樣的

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,而不是 CheckBox。

您可以將單元格值轉換為布爾值,然后檢查它,如下所示:

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

你需要做這樣的事情:

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

'throw the message

end if

只有第三個示例對我有用,但我必須添加一個 Timer

Private Sub DgvElencoFile_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvElencoFile.CellClick

    'Variabili gestione programma
    Dim numerocolonnaSelezionata As Integer
    Dim numeroColonnaSelezionataPerDowonload As Integer
    Dim numeroRigaSelezionata As Integer

    numeroRigaSelezionata = e.RowIndex
    NumerocolonnaSelezionata = e.ColumnIndex

    If NumeroRigaSelezionata < 0 Then
        ClaFunzSgnSon.SegnaleSonoro(ClaFunzSgnSon.EnTipoSgnSon.ErroreImpostazioneDati)
        GoTo FineSubFunz
    End If
    numeroColonnaSelezionataPerDowonload = -1

    If DgvElencoFile.Rows(numeroRigaSelezionata).Cells(ClnDownLoad.Name).Selected Then numeroColonnaSelezionataPerDowonload = numerocolonnaSelezionata

    If numeroColonnaSelezionataPerDowonload >= 0 Then
        TimVisCheckDownLoad.Start()
    End If

FineSubFunz:結束子

Private Sub TimVisCheckDownLoad_Tick(sender As Object, e As EventArgs) 處理 TimVisCheckDownLoad.Tick TimVisCheckDownLoad.Stop()

    Dim isTickedOn = CBool(DirectCast(DgvElencoFile.CurrentCell, DataGridViewCheckBoxCell).EditingCellFormattedValue)

    If isTickedOn Then
        MessageBox.Show("You ticked the box.")
    Else
        MessageBox.Show("You cleared the box.")
    End If

End Sub

我找到了一個簡單的解決方案。

單擊單元格后只需更改單元格焦點。

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) 索引的列是否存在。

將 dataGridView 中的網格類型設置為“DataGridViewCheckBoxXColumn”而不是 DataGridViewCheckBoxColumn。

所有的問題都會得到解決

帖子很舊,但它可以在需要時提供幫助:鑄造單元后,您具有以下三個屬性:

Private Sub YourDataGridView_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles YourDataGridView.CellMouseClick

    Dim b, b1, b2 As Boolean
    b = DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditingCellValueChanged
    b1 = CBool(DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditingCellFormattedValue)
    b2 = CBool(DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditedFormattedValue)
End Sub

自從發送此問題以來已經有很長時間了,但是對於遇到相同問題的任何人來說,這個答案都會很有用。 就我而言,我使用(我正在使用您的符號):

dgvChooseQs.Item(6, vCont).State

其中 6 是復選框列號,在我的例子中是第 6 列。 vCont 是 FOR NEXT 循環中的迭代計數器。 如果 State 等於 32,則選中復選框,否則 State 將為零。 我希望這能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM