繁体   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