遍历DataGridView行时未获取所有行 [英] Not getting all rows when iterating through DataGridView rows

查看:120
本文介绍了遍历DataGridView行时未获取所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a VB.NET WinForms application I have a DataGridView with a checkbox as a non-bound column in the first column. I need to add to a collection of rows each row that has its checkbox checked.

我正在使用以下代码遍历各行,并找到带有选中复选框的行:

I'm using the below code to iterate through the rows and find the ones with a checked checkbox:

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

但是它缺少带有选中复选框的最后一行. IE.如果我选中三个复选框,则在控制台输出中,我看到前两个选中行的"Emp No".

But it is missing the last row with a checked checkbox. I.e. If I check three checkboxes, in the console output I'm seeing the "Emp No" of the first two checked rows.

认为它就像零索引的问题一样,我也尝试使用计数器进行迭代:

Thinking it was acting like an issue with zero indexing, I also tried doing an iteration that used a counter:

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

但是我得到与该代码相同的行为.我尝试更改Integer = 0和Rows.Count-1等,但这也无济于事.

But I get the same behavior with that code. I tried changing around the Integer = 0 and Rows.Count - 1, etc but that didn't help either.

万一重要,则需要将DataGridView的SelectionMode设置为CellSelect.

In case it matters, the DataGridView's SelectionMode needs to be set to CellSelect.

更新

表中有 694 条记录是填充datagridview的记录.

There are 694 records in the table that the datagridview is being populated by.

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

I added a console output to get the rows.count value:

Console.WriteLine("Num rows: " & dgvEmployees.Rows.Count)

并得到 695 ,这是有道理的,因为datagridview中的最后一行允许输入新行. (但是我本以为第二种迭代方法中的Count-1可以解决这个问题.)

and got 695, which makes sense due to the last row in the datagridview is there to allow entry of a new row. (But I would have thought that the Count - 1 in the second iteration method would account for that.)

我还添加了

Console.WriteLine("Row index: " & chkcell.RowIndex)

就在如果选中"复选框之前,并且选中了第一,第三和第五行(索引0、2、4),则输出如下:

right before the If check for a checked checkbox and, with the first, third and fifth rows checked (indexes 0, 2, 4), here's what was output:

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 is be ..."输出.

There should have been a "this should be..." output after the Row index: 4 line.

推荐答案

看来,DataGridView并未提交您选中的最后一个复选框,因为您没有将焦点移到另一个单元格上.在运行迭代代码之前,请尝试以下操作:

It appears that the last check box that you check is not being committed by the DataGridView because you aren't moving the focus to another cell after checking it. Try this before running your iteration code:

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

这篇关于遍历DataGridView行时未获取所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆