在Vb.net中打破声明

时间:2015-05-16 08:34:12

标签: vb.net visual-studio-2010 datagridview vb.net-2010

如果满足这个条件,如何打破这个陈述?

Val(DataGridView1.Rows(x).Cells(7).Value) <= 2 

然后在满足上述声明之后,它应继续使用此声明

DataGridView1.Rows(x).Cells(7 + 1).Value = DataGridView1.Rows(x).Cells(7).Value

这是我的完整代码:

    Try

        For x As Integer = 0 To DataGridView1.Rows.Count - 1

            'Strategy 1
            If Val(DataGridView1.Rows(x).Cells(7).Value) <= 2 Then

                DataGridView1.Rows(x).Cells(7 + 1).Value = DataGridView1.Rows(x).Cells(7).Value - 2

            Else
                DataGridView1.Rows(x).Cells(7 + 1).Value = DataGridView1.Rows(x).Cells(7).Value

            End If


        Next


    Catch ex As Exception

    End Try

2 个答案:

答案 0 :(得分:0)

如果您想在某项工作后退出循环,可以使用Exit Statement (Visual Basic)

答案 1 :(得分:0)

因为无论如何你都需要循环所有行 -
使用布尔变量来标记已找到所需的值

Try

    Dim valueFound As Boolean = False
    For x As Integer = 0 To DataGridView1.Rows.Count - 1
        If Val(DataGridView1.Rows(x).Cells(7).Value) <= 2 Then
            If valueFound = False Then
                DataGridView1.Rows(x).Cells(7 + 1).Value = DataGridView1.Rows(x).Cells(7).Value - 2
                valueFound = True
            Else
                DataGridView1.Rows(x).Cells(7 + 1).Value = DataGridView1.Rows(x).Cells(7).Value
            End If
        Else
            'Do something if value is not <= 2
        End If
    Next

Catch ex As Exception

End Try