在datagridview中查找可见行的值

时间:2017-06-20 19:02:39

标签: vb.net

我能够获得正确的行数,但问题是我在循环中运行。

OrdNum = dgvWsNorth.Rows(i).Cells("ORDNUM").Value.ToString()

此行将遍历所有可见和不可见行=到行计数。

我需要它只遍历可见行=到行数。

Dim AllRows As Integer = dgvWsNorth.Rows.GetRowCount(DataGridViewElementStates.Visible)
        Dim OrdNum As Integer
        Dim LinNum As Integer
        Dim UnitPriority As Integer
        Dim Ws_N As Integer = My.Settings.NorthLine

        For i = 0 To AllRows - 1

            OrdNum = dgvWsNorth.Rows(i).Cells("ORDNUM").Value.ToString()
            LinNum = dgvWsNorth.Rows(i).Cells("LINE").Value.ToString()


            If IsDBNull(dgvWsNorth.Rows(i).Cells("UNIT_PRIORITY").Value) Then
                UnitPriority = 999
            Else
                UnitPriority = dgvWsNorth.Rows(i).Cells("UNIT_PRIORITY").Value.ToString()
            End If

                clsScheduler.UPDATE_ASSIGNED_WS(Ws_N, OrdNum, LinNum, clsLogin.plant_id, clsLogin.dept_id, UnitPriority)

        Next

2 个答案:

答案 0 :(得分:1)

如何循环遍历所有行,并仅计算可见的行?

    Dim i = 0
    Dim OrdNum As Integer
    Dim LinNum As Integer
    Dim UnitPriority As Integer
    Dim Ws_N As Integer = My.Settings.NorthLine
    For Each r As DataGridViewRow In dgvWsNorth.Rows

        If r.Visible Then

            OrdNum = r.Cells("ORDNUM").Value.ToString()
            LinNum = r.Cells("LINE").Value.ToString()


            If IsDBNull(r.Cells("UNIT_PRIORITY").Value) Then
                UnitPriority = 999
            Else
                UnitPriority = r.Cells("UNIT_PRIORITY").Value.ToString()
            End If

            clsScheduler.UPDATE_ASSIGNED_WS(Ws_N, OrdNum, LinNum, clsLogin.plant_id, clsLogin.dept_id, UnitPriority)

            i += 1
        End If
    Next

答案 1 :(得分:0)

实际上,我决定试一试,我认为你可以用你的每一个声明的Lambda做到这一点

而不是for for next循环使用a表示lambda表达式

For each r as datagridviewrow in DataGridView1.Rows.Cast(Of Datagridviewrow)().Where(Function(row) row.Visible = true)

... your loop code using r instead of rows(i)

Next

这应该只遍历可见的行。