คอลัมน์ว่างเปล่าจึงเกิดข้อผิดพลาดเนื่องจากการแปลงจากสตริงเป็นประเภท 'Double' ไม่ถูกต้อง

การใช้งานวีบีเน็ต

หากค่าเซลล์กริดว่างเปล่า ฉันได้รับข้อผิดพลาดเนื่องจาก "การแปลงจากสตริง "" เป็นประเภท 'Double' ไม่ถูกต้อง"

รหัส (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


person Gopal    schedule 22.10.2011    source แหล่งที่มา


คำตอบ (2)


ตามที่แนะนำ ให้ใช้วิธี 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

และอื่นๆ ทั้งหมด

ความนับถือ.

person Carmelo La Monica    schedule 22.10.2011

ใช้วิธี TryParse

Integer.TryParse(text,intVar)
Double.TryParse(text,doubleVar)
person kv-prajapati    schedule 22.10.2011