【问题标题】:Find rows with CheckBox checked DataGridView查找选中复选框的行 DataGridView
【发布时间】:2017-11-02 21:21:12
【问题描述】:

我输入了一些代码来查找在我的 DataGridView 中选中了哪些复选框,但由于某种原因这不起作用。

我已经遍历 DataGridView 中的行:

For Each row As DataGridViewRow In dgv_assets.Rows

Next

然后在这里我将第一列转换为 DataGridViewCheckBoxCell:

For Each row As DataGridViewRow In dgv_assets.Rows

    Dim chk As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell)

Next

然后我正在检查所有已选中的复选框:

For Each row As DataGridViewRow In dgv_assets.Rows

    Dim chk As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell)

    If chk.Value = chk.TrueValue Then
        MessageBox.Show("Checked")
    End If

Next

由于某种原因,即使复选框被选中或取消选中,它们都会点击 MessageBox。

【问题讨论】:

    标签: vb.net checkbox datagridview


    【解决方案1】:

    你的代码几乎是正确的,我猜是转换的问题。

    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim chk As DataGridViewCheckBoxCell = row.Cells(Column1.Name)
        If chk.Value IsNot Nothing AndAlso chk.Value = True Then
            MessageBox.Show("Checked: " + chk.RowIndex.ToString())
        End If
    Next
    

    Column1 应该是您所引用的DataGridViewCheckBoxCell 的列名。

    【讨论】:

    • 您应该使用AndAlso 而不是And,因为它是短路的,这意味着如果左侧评估为False,它将不会评估右侧。这有助于防止NullReferenceExceptions。只是一个小提示。 :)
    • @VisualVincent 感谢您的提示,我已经更改了代码。
    【解决方案2】:

    你可以用这种简单的方式来做。

       For i As Integer = 0 To dtg.RowCount - 1
            If dtg.Item(0, i).Value = "True" Then
                MsgBox("check")
            End If
        Next
    

    希望对你有所帮助

    【讨论】:

    • 解释在哪里?为什么 OP(或其他有同样问题的人)要使用它?见How to Answer
    【解决方案3】:

    这里是一个使用语言扩展方法的例子

    表单级变量

    Private Const CheckBoxColName As String = "Process"
    

    将以下内容放在代码模块中,而不是表单或类中

    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Public Function CheckBoxCount(ByVal GridView As DataGridView, ByVal ColumnIndex As Integer, ByVal Checked As Boolean) As Integer
       Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count
    End Function
    

    使用扩展方法使用上面的私有变量CheckBoxColName,它是DataGridView中列的名称作为DataGridViewCheckBoxColumn

    If DataGridView1.CheckBoxCount(CheckBoxColName, True) > 0 Then
        Dim Rows = DataGridView1.GetCheckedRows1(CheckBoxColName)
        For Each Row In Rows
            Console.WriteLine(Row.Cells.Item(1).Value)
        Next
    End If
    

    如果你想使用列索引而不是列名,下面会这样做

    <System.Diagnostics.DebuggerStepThrough()> _
    <Runtime.CompilerServices.Extension()> _
    Public Function CheckBoxCount(ByVal GridView As DataGridView, ByVal ColumnIndex As Integer, ByVal Checked As Boolean) As Integer
       Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count
    End Function
    

    注意两者都允许您检查或取消检查。

    以下获取实际行

       <System.Diagnostics.DebuggerStepThrough()> _
       <Runtime.CompilerServices.Extension()> _
       Public Function GetCheckedRows1(ByVal GridView As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow)
          Dim Temp = (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow).ToList
          Return (From SubRows In Temp Where CBool(SubRows.Cells(ColumnName).Value) = True).ToList
       End Function
    

    【讨论】:

      【解决方案4】:

      您必须首先验证 TrueValue 不是 null ,因为根据文档,默认值为 null。然后检查是否为真。

      这里文档:TrueValue

      【讨论】:

        【解决方案5】:

        添加名称为 CK1 的 CheckBox 并添加名称为 Dgrd 的 DataGridView 并且第一个单元格必须是 DataGridViewCheckBoxCell 并添加代码:

          Private Sub CK1_CheckedChanged(sender As Object, e As EventArgs) Handles CK1.CheckedChanged
            If CK1.Checked = True Then
                Try
                    Dim I As Integer
                    For I = 0 To Dgrd.Rows.Count - 1
                        Dim CHKRow As DataGridViewCheckBoxCell = Dgrd.Rows(I).Cells(0)
                        If CHKRow.Value = False Then
                            CHKRow.Value = True
                        End If
                    Next
                Catch ex As Exception
                End Try
            Else
                Try
                    Dim I As Integer
                    For I = 0 To Dgrd.Rows.Count - 1
                        Dim CHKRow As DataGridViewCheckBoxCell = Dgrd.Rows(I).Cells(0)
                        If CHKRow.Value = True Then
                            CHKRow.Value = False
                        End If
                    Next
                Catch ex As Exception
                End Try
            End If
        End Sub
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-08
          • 1970-01-01
          • 2011-02-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多