2017-06-20 61 views
0

我能夠獲得正確的行數,但問題是當我運行循環時。在datagridview中查找可見行的值

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 
+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 
+0

正是我需要的!謝謝 –

0

其實,我決定給這個一杆,我想你可以用LAMBDA您爲每個語句

取而代之的是爲下一個循環使用的每一個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 

這應該只遍歷可見的行。

相關問題