BlakeMcKenna
asked on
How to properly check DataGridViewRow data when validating?
I have a simple edit routine for my DataGridView. It basically loops thru each row checking each column to make sure there is data in it. However, if a column has empty data and it's a date or numeric field, I get the following error:
"Conversion from type 'DBNull' to type 'Integer' is not valid".
I'm pretty sure I need to do some kind of conversion but not sure how to syntactically set it up. Here is my edit routine below.
Private Function EditsPassed() As Boolean
Dim blnPassed As Boolean = True
Try
For Each row As DataGridViewRow In dgvTrans.Rows
If IsDBNull(row.Cells(3).Valu e) Then
blnPassed = False
ElseIf IsDBNull(row.Cells(4).Valu e) Then
blnPassed = False
ElseIf IsDBNull(row.Cells(5).Valu e) Then
blnPassed = False
ElseIf IsDBNull(row.Cells(6).Valu e) Then
blnPassed = False
ElseIf IsDBNull(row.Cells(7).Valu e) Then
blnPassed = False
End If
Next
Catch ex As Exception
Dim strErr As String = "frmTransactions/EditsPass ed() - " & ex.Message
MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
blnPassed = False
End Try
Return blnPassed
End Function
"Conversion from type 'DBNull' to type 'Integer' is not valid".
I'm pretty sure I need to do some kind of conversion but not sure how to syntactically set it up. Here is my edit routine below.
Private Function EditsPassed() As Boolean
Dim blnPassed As Boolean = True
Try
For Each row As DataGridViewRow In dgvTrans.Rows
If IsDBNull(row.Cells(3).Valu
blnPassed = False
ElseIf IsDBNull(row.Cells(4).Valu
blnPassed = False
ElseIf IsDBNull(row.Cells(5).Valu
blnPassed = False
ElseIf IsDBNull(row.Cells(6).Valu
blnPassed = False
ElseIf IsDBNull(row.Cells(7).Valu
blnPassed = False
End If
Next
Catch ex As Exception
Dim strErr As String = "frmTransactions/EditsPass
MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
blnPassed = False
End Try
Return blnPassed
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the answer. As far as returning False as soon as the condition is met, I usually do that kind of logic but just forgot here. Thanks for pointing that out!
Cheers!