简体   繁体   中英

datagridview cell null value error

If the value in datagridview cell is not entered the only way to handle the error is this for now. Anyone have better solution ?

Dim senderGrid = DirectCast(sender, DataGridView)

If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn _
        AndAlso e.RowIndex >= 0 _ 
        AndAlso senderGrid.Columns(e.ColumnIndex).Name = "Add" Then

    Dim _status As Integer

    Try
        _status = Math.Abs(CInt(dgv6.Item("dgv6_Aktivno", dgv6.CurrentRow.Index).Value))
    Catch ex As Exception
        _status = 0
    End Try

    Dim _id As Integer
    Try
        _id = dgv6.Item("dgv6_id", dgv6.CurrentRow.Index).Value
    Catch ex As Exception
        _id = 0
    End Try

End if

As you are converting a value to Integer and setting 0 as its default value, you can try Convert.ToInt32 to handle null values but with some precautions. Convert.ToInt32 converts any null values as 0

_status = Math.Abs(Convert.ToInt32("VALUE_TO_CONVERT"))

And the precautions:

  1. VALUE_TO_CONVERT must of type String .
  2. You may still need to handle the cases where VALUE_TO_CONVERT contains any symbols like , , . , () or chars.
  3. You also need to take care if VALUE_TO_CONVERT is of type Char

For details discussion on conversions check whats-the-main-difference-between-int-parse-and-convert-toint32 and also the MSDN for Convert.ToInt32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM