检查datagridview是否包含整数的更好方法?

时间:2018-07-11 14:11:11

标签: vb.net datagridview integer

所以目前我有一个datagridview,我正在将一个excel文件导入其中。第3-6列应包含数字。

这是我到目前为止的代码..但是必须有一种更快速,更有效的数字查询方式? 也许是带有isinteger的东西。 而且这只会检查一列。

isFound变量是一个布尔值,用于检查导入的excel文件中是否存在某个字符串。 因此,例如,如果excel文件在某个单元格中包含单词“ data”,则它将依次标记isFound = true。

For Each row As DataGridViewRow In DataGridView1.Rows
            If Not row.Cells("F3").Value Is DBNull.Value Then

                If (isFound) Then
                    'MessageBox.Show("Data Exists!")
                    Select Case row.Cells("F3").Value
                    'checks for all numbers 0 - 9999
                        Case "1", "2", "3", "4", "5", "6" .. "9999"

                            'All pass verification, Do nothing
                        Case Else
                            'Point out the wrong value 
                            row.Cells("F3").Style.BackColor = Color.Red
                                                    End Select
                Else
                    '    MessageBox.Show("Not a number!")
                End If
            End If
        Next

2 个答案:

答案 0 :(得分:3)

您可以使用Integer.TryParse。它需要一个字符串和一个数字。如果可以正确转换,则返回true并将值放在number变量中。

    For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.Cells("F3").Value Is DBNull.Value Then

            If (isFound) Then
                Dim cellNumber As Integer

                If Integer.TryParse(row.Cells("F3").Value, cellNumber) AndAlso cellNumber >= 0 AndAlso cellNumber <= 9999 Then
                    'All pass verification, Do nothing
                Else
                    'Point out the wrong value 
                    row.Cells("F3").Style.BackColor = Color.Red
                End If
            Else
                '    MessageBox.Show("Not a number!")
            End If
        End If
    Next

另一个想法是创建一个执行所有检查的函数,而只是调用该函数。

Private Function IsInRange(ByVal numberAsString As String, ByVal min As Integer, ByVal max As Integer) As Boolean

    Dim number As Integer

    If Not Integer.Parse(numberAsString, number) Then
        Return False
    End If

    If number < min Or number > max Then
        Return False
    End If

    Return True
End Function

...

            If IsInRange(row.Cells("F3").Value, 0, 9999) Then
                'All pass verification, Do nothing
            Else
                'Point out the wrong value 
                row.Cells("F3").Style.BackColor = Color.Red
            End If

答案 1 :(得分:-1)

此解决方案并不完美,但仍然更好。。。我本可以使用IsNumeric,但是可以加倍。您可以使用IsNumeric,也可以使用cint(cdbl(s))= cdlb(s)

For Each row As DataGridViewRow In DataGridView1.Rows
    If Not row.Cells("F3").Value Is DBNull.Value Then
        If (isFound) Then
            'MessageBox.Show("Data Exists!")
            dim s  as string =  row.Cells("F3").Value
            for i as integer = 0 to 9
                s= strings.replace(s,cstr(i),"")
            next
            if strings.len(  row.Cells("F3").Value)<=4 andalso s="" then
                'it IS an integer <=9999
            Else
                'Point out the wrong value 
                row.Cells("F3").Style.BackColor = Color.Red

        Else
            '    MessageBox.Show("Not a number!")
        End If

    Next

编辑: 替代:

  dim s  as string = Select Case row.Cells("F3").Value
  if isnumeric(s) andalso cdbl(s)=cint(cdbl(s)) andalso cint(s)<=9999 then
                'it IS an integer <=9999