【问题标题】:Break statement in Vb.netVb.net 中的中断语句
【发布时间】:2015-07-28 04:56:25
【问题描述】:

如果满足这个条件,如何打破这个语句?

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

【问题讨论】:

  • BREAK 是什么意思?你的意思是你想退出循环?你的意思是你想停止执行?如果在 Col 7 中不止一行的值小于 2,会发生什么情况?请澄清中断的含义...使用上面的代码,它将按照您的意愿进行,然后继续下一行,直到处理完所有行...

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


【解决方案1】:

如果你想在完成一些工作后退出循环,你可以使用Exit Statement (Visual Basic)

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-11
      • 2011-11-13
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多