2012-11-20 83 views
-1

我行通過在一個DataGridView循環如下:如何跳過DataGridViewRow?

For Each orow As DataGridViewRow In GV_NS.Rows 
    If orow.Cells(0).Value.Length = 0 Then 
     //Skip this row and go to next row 
    Else 
     //do this 
    End If 
Next 

我希望能跳到下一行,如果第一列是空的。我試過在If orow.Cells(0).Value.Length = 0中只使用Next,但是它引發了語法錯誤If must end with matching End If。有什麼建議麼?

回答

1

你也可以只扭轉你的測試。

For Each orow As DataGridViewRow In GV_NS.Rows 
    If orow.Cells(0).Value.Length <> 0 Then 
     //do this 
    End If 
Next 
+0

我不敢相信我忽略了這一點。謝謝。 – TimeBomb006

1

要跳到一個ForDo,或While循環的下一次迭代中,使用Continue像這樣:

For Each orow As DataGridViewRow In GV_NS.Rows 
    If orow.Cells(0).Value.Length = 0 Then 
     //Skip this row and go to next row 
     Continue For 
    Else 
     //do this 
    End If 
Next 
+1

你也可以刪除else和後寫代碼的'完If',避免深度嵌套。 –

0
'Filter rows with LINQ 
    Dim Query = From row In GV_NS.Rows 
     Where CType(row, DataGridViewRow).Cells(0).Value.ToString.Length > 0 
     Select row 
    For Each Row As DataGridViewRow In CType(Query.ToArray, DataGridViewRow()) 
     'Do something 
    Next