vb.net 使用 CheckBox 选中 DataGridView 查找行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34437662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:39:15  来源:igfitidea点击:

Find rows with CheckBox checked DataGridView

vb.netcheckboxdatagridview

提问by Ben Clarke

I put some code in to find what checkboxes have been checked in my DataGridView, but for some reason this isn't working.

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

I have looping through the rows in the DataGridView:

我循环遍历 DataGridView 中的行:

For Each row As DataGridViewRow In dgv_assets.Rows

Next

Then in here i have casted the first column as a DataGridViewCheckBoxCell:

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

For Each row As DataGridViewRow In dgv_assets.Rows

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

Next

Then I am checking for all the checkboxes that have been checked:

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

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

For some reason even if the checkboxes are checked or unchecked they all hit the MessageBox.

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

回答by Ruwanka Madhushan

Your code is almost correct, I guess the casting is the problem.

你的代码几乎是正确的,我猜是演员阵容的问题。

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

Column1should be the column name of the DataGridViewCheckBoxCellthat you are referring.

Column1应该是DataGridViewCheckBoxCell您所指的列名。

回答by MBahamondes

you must first verify that TrueValue not null , since according to the documentation , the default is null. And then check if true.

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

Here Doc : TrueValue

这里文档:TrueValue

回答by Aladein

Add CheckBox with Name CK1 And Add DataGridView with Name Dgrd and must the first cell be DataGridViewCheckBoxCell And Add the code:

添加名称为 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

回答by lucap.13

you can do in that simple way.

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

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

I hope to be as helpful

我希望能有帮助

回答by Karen Payne

Here is an example using a language extension method

这是使用语言扩展方法的示例

Form level variable

表单级变量

Private Const CheckBoxColName As String = "Process"

Place the following in a code module, not a form or class

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

<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

Use the extension method using the private variable above, CheckBoxColName which is the name of the column in the DataGridView as a DataGridViewCheckBoxColumn

使用上述私有变量的扩展方法,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

If you want to use column index rather than column name the following will do so

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

<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

Note both allow you to get check or un-checked.

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

The following gets the actual rows

以下获取实际行

   <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