I am having some trouble with this code
Expand|Select|Wrap|Line Numbers
- Private Sub VehDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles VehDataGridView.CellValidating
- Dim row As Int16 = 0
- Dim numrows As Int16 = Me.VehDataGridView.Rows.Count
- Dim angle As Int16
- While row + 1 < numrows
- Dim rownum As Int16 = 1 + row
- angle = Me.VehDataGridView.Item(2, row).Value
- checkangle(angle, rownum, row)
- row += 1
- End While
- End Sub
- Sub checkangle(ByVal angle As Int16, ByVal rownum As Int16, ByVal row As Int16)
- If angle > 360 Or angle < 0 Then
- Dim message As String = " Pleese enter a angle between 0 and 360 in row " & rownum
- MessageBox.Show(message, "input error", MessageBoxButtons.OK)
- Me.VehDataGridView.Item(2, row).Value = 0
- End If
- End Sub
When a new row is created in the datagrid it returns an exception
System.InvalidCastException was unhandled
Message="Conversion from type 'DBNull' to type 'Short' is not valid."
On the line
“angle = Me.VehDataGridView.Item(2, row).Value”
I can’t figure out how to handel this excepton.
What I want is, if the value is null the sub checkangle not be called
So far I cant seem to be able determine when the value of Me.VehDataGridView.Item(2, row) is null.
My guess is the answer is obvious but I cant seem to figure it out.
Thank you for your help