Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

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).Value) Then
                    blnPassed = False
                ElseIf IsDBNull(row.Cells(4).Value) Then
                    blnPassed = False
                ElseIf IsDBNull(row.Cells(5).Value) Then
                    blnPassed = False
                ElseIf IsDBNull(row.Cells(6).Value) Then
                    blnPassed = False
                ElseIf IsDBNull(row.Cells(7).Value) Then
                    blnPassed = False
                End If
            Next

        Catch ex As Exception
            Dim strErr As String = "frmTransactions/EditsPassed() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
            blnPassed = False
        End Try

        Return blnPassed
    End Function
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlakeMcKenna

ASKER

James,

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!