我放了一些代码来查找我的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。
答案 0 :(得分: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
的列名。
答案 1 :(得分:0)
你可以用这么简单的方式做。
For i As Integer = 0 To dtg.RowCount - 1
If dtg.Item(0, i).Value = "True" Then
MsgBox("check")
End If
Next
我希望能有所帮助
答案 2 :(得分:0)
以下是使用语言扩展方法的示例
表单级变量
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
答案 3 :(得分:0)
您必须首先验证TrueValue不为null,因为根据文档,默认值为null。然后检查是否为真。
此处文件:TrueValue
答案 4 :(得分:0)
使用名称CK1添加CheckBox并使用Name 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