Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在遍历DataGridView行时未获取所有行_Vb.net_Winforms_Datagridview_Datagridviewrow - Fatal编程技术网

Vb.net 在遍历DataGridView行时未获取所有行

Vb.net 在遍历DataGridView行时未获取所有行,vb.net,winforms,datagridview,datagridviewrow,Vb.net,Winforms,Datagridview,Datagridviewrow,在VB.NET WinForms应用程序中,我有一个DataGridView,第一列中有一个复选框作为非绑定列。我需要将选中复选框的每一行添加到行集合中 我正在使用下面的代码遍历这些行,并找到带有复选框的行: For Each row As DataGridViewRow In dgvEmployees.Rows Dim chkCell As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBox

在VB.NET WinForms应用程序中,我有一个DataGridView,第一列中有一个复选框作为非绑定列。我需要将选中复选框的每一行添加到行集合中

我正在使用下面的代码遍历这些行,并找到带有复选框的行:

For Each row As DataGridViewRow In dgvEmployees.Rows

    Dim chkCell As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell)
    If Convert.ToBoolean(chkCell.Value) = True Then
        Console.WriteLine("This should be the Emp No of the checked row: " & row.Cells(1).Value.ToString())
    End If

Next
但它缺少带有复选框的最后一行。也就是说,如果我选中三个复选框,在控制台输出中,我看到前两个选中行的“Emp No”

我认为这就像是一个零索引问题,我还尝试使用计数器进行迭代:

For rowNum As Integer = 0 To dgvEmployees.Rows.Count - 1
    Dim currentRow As DataGridViewRow = dgvEmployees.Rows(rowNum)
    Dim chkCell As DataGridViewCheckBoxCell = DirectCast(currentRow.Cells(0), DataGridViewCheckBoxCell)

    If Convert.ToBoolean(chkCell.Value) = True Then
        Console.WriteLine("This should be the emp no of the checked row: " & currentRow.Cells(1).Value.ToString())
    End If

Next
但我在代码中得到了相同的行为。我试着改变整数=0和Rows.Count-1,等等,但也没用

如果有必要,DataGridView的SelectionMode需要设置为CellSelect

更新

datagridview填充的表中有694条记录

我添加了控制台输出以获取rows.count值:

Console.WriteLine("Num rows: " & dgvEmployees.Rows.Count)
并且得到了695,这是有意义的,因为datagridview中的最后一行允许输入新行。(但我认为第二次迭代方法中的Count-1可以解释这一点。)

我还补充说

Console.WriteLine("Row index: " & chkcell.RowIndex)
在If检查复选框之前,选中第一行、第三行和第五行(索引0、2、4),输出如下:

Num rows: 695
Row index: 0
this should be the emp no of the checked row: ACS175
Row index: 1
Row index: 2
this should be the emp no of the checked row: AJAW03
Row index: 3
Row index: 4
Row index: 5
Row index: 6

行索引:4行之后应该有一个“this should be…”输出。

您选中的最后一个复选框似乎没有被DataGridView提交,因为您在选中后没有将焦点移动到另一个单元格。在运行迭代代码之前,请尝试以下操作:

If dgvEmployees.IsCurrentCellDirty Then
    dgvEmployees.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If 

最后选中的行是网格中的最后一行吗?您能否逐步使用调试器,查看dgveEmployees.Rows.Count的值是否与您在网格中看到的行数匹配?@CaseyWilkins,谢谢您,我更新了我的问题,详细介绍了迭代过程中的行数和索引输出。您是否尝试过使用调试器逐步完成前5或6次迭代,检查chkCell.Value作为你的一步吗?我刚刚检查了,这是最奇怪的事情:我一行一行地迭代For/Next循环,当我使用最后一个复选框(仍然使用上面的示例,所以我在行索引4)到达行时,chkCell.Value显示为False,当该复选框被选中时。前两个复选框正确显示为True。-最后一个复选框的计算结果总是False。在选中最后一行的复选框后,是否移动/单击另一行?在检查最后一行时,datagridview似乎没有提交更改。尝试检查最后一行,然后单击任何其他行/单元格以移动焦点。然后仔细检查你的代码,看看这个值是否为真。没错,凯西。(虽然感觉有点像黑客)非常感谢你抽出时间。