列为空然后出现错误,因为从字符串“”转换为类型'Double'无效

时间:2011-10-22 05:10:06

标签: vb.net visual-studio

使用VB.Net

如果网格单元格值为空,我收到错误,因为“从字符串转换”“到'双''类型无效”

代码(gridview_CellLeave)

Dim z1, z2, z3, z4 As Int32


        If grvList.CurrentRow.Cells(1).Value <> "" Then
            z1 = grvList.CurrentRow.Cells(1).Value
        End If
        If grvList.CurrentRow.Cells(2).Value <> "" Then
            z2 = grvList.CurrentRow.Cells(2).Value
        End If
        If grvList.CurrentRow.Cells(3).Value <> "" Then
            z3 = grvList.CurrentRow.Cells(3).Value
        End If
        If grvList.CurrentRow.Cells(4).Value <> "" Then
            z4 = grvList.CurrentRow.Cells(4).Value
        End If

如何解决此错误。

需要Vb.net代码帮助

2 个答案:

答案 0 :(得分:1)

使用TryParse方法。

Integer.TryParse(text,intVar)
Double.TryParse(text,doubleVar)

答案 1 :(得分:1)

建议使用TryParse方法,因此避免使用try / catch,因为该方法已经处理了任何格式错误。

 Dim z1, z2, z3, z4 As Int32

    If Integer.TryParse(grvList.CurrentRow.Cells(1).Value, z1) Then
        'your code here
    End If

     Dim z1, z2, z3, z4 As Double

    If Double.TryParse(grvList.CurrentRow.Cells(1).Value, z1) Then
        'your code here
    End If

所以其他所有

问候。