如何跳过DataGridViewRow?

时间:2012-11-20 16:44:55

标签: vb.net

我循环遍历DataGridView中的行,如下所示:

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

如果第一列为null,我希望能够跳到下一行。我尝试在Next中使用If orow.Cells(0).Value.Length = 0,但却抛出了语法错误If must end with matching End If。有什么建议吗?

3 个答案:

答案 0 :(得分:1)

要跳到ForDoWhile循环的下一次迭代,请使用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 :(得分:1)

你也可以改变你的测试。

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

答案 2 :(得分: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