Link to home
Start Free TrialLog in
Avatar of rwallacej
rwallacej

asked on

VB.net datagridview dataerror - handle an exception

A datagridview throws  System.data.nonullallowexception if user doesn''t enter value in field "Year"

How do I deal with this "graciously" - i.e. display a message to say "Please enter year", and jump to the "Year" field?
That's nicer than the row just getting deleted by form

Thanks
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Handle the DataGridView.DataError event.

Bob
Avatar of rwallacej
rwallacej

ASKER

ok, I got this far with DataError

        If TypeOf e.Exception Is System.Data.NoNullAllowedException Then
            e.Cancel = True
            MessageBox.Show("Period must have a value!", "PI-e2 Emissions Forecaster", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ElseIf TypeOf e.Exception Is System.Data.ConstraintException Then
            e.Cancel = True
            MessageBox.Show("Period must be unique!", "PI-e2 Emissions Forecaster", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If


HOW DO I select the Period field for user to enter period?

thanks
>>HOW DO I select the Period field for user to enter period?
Do you mean the Period cell?

Bob
yes, I mean the period cell - sorry
it would also be good if as well as selecting the period cell, it was automatically set to be the maximum in the Period column + 1
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America 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
           DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0)
did it