How do I (cleanly) dispose of a row in DataGridView DataError even

G

Guest

I'm writing a Winforms project in VB/Dot Net 2.0 and I'm having some trouble
handling the Data Error event.

The grid is bound to a filtered binding source. Part of the primary key is
set by one of the cells in the grid which is in combobox mode. Add is
enabled.

What I want to do in DataError is give the user the choice of remaining in
the row to fix it (e.cancel = true) or just dispose of the changes. If it is
an existing row, virtually anything seems to work fine (such as
<bindingsource.ResetBindings, or nothing for that matter.)

What gives me trouble is a newly added row. Any attemp to get rid of the new
row by any means seems to get me into some sort of infinite loop, usually
with errors like 'Index <number> does not have a value'.

Am I even trying to handle the error in the right place? RowValidating
doesn't seem to do anything for index errors.
 
G

Guest

I may be missing something here. The row does get validated as far as
routine non-key fields are concerned. No problem there.

However through trial and error I did find a solution to my problem, in the
DataError handler, which essentially consists of the thing ignoring those
IndexOutOfRangeExceptions I mentioned. Here's the code. Does this make
sense?

Private Sub dgFlowComp_DataError(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles
dgFlowComp.DataError
Dim nReturnCode As Integer = MsgBoxResult.Yes
Dim nDialogStyle As Integer = CType(MsgBoxStyle.YesNo, Integer) +
CType(MsgBoxStyle.Exclamation, Integer)

If TypeOf (e.Exception) Is ConstraintException Then
nReturnCode = MsgBox("Error editing grid, error = " +
e.Exception.Message + ". Do you wish to discard changes?",
CType(nDialogStyle, Microsoft.VisualBasic.MsgBoxStyle), "Warning")
ElseIf Not TypeOf (e.Exception) Is IndexOutOfRangeException Then
MsgBox("Error editing grid, error = " + e.Exception.Message)
End If

If nReturnCode = MsgBoxResult.Yes Then
dgFlowComp.CancelEdit()
End If

e.Cancel = True

End Sub
 
B

Brendon Bezuidenhout

Hmmm - With regards to the IndexOutOfRangeException and your BindingSource:

Are you using the AddingNew event or AddNew method on the binding source?
You most likely have just not added the record to the underlying DataSet but
the BindingManager thinks it is there or visa versa...

Sorry I can't be more help :(

Brendon

p.s. - Strings are immutable - use StringBuilder rather for string
concatenation :) it will speed things up and use Messagebox.Show :) rather
than VB6 msgbox *grin* or having to do messy type conversions which "could"
be error prone...

"Messagebox.Show(title, message, buttons, icons)"
 
G

Guest

My turn to be sorry. I'm not really using anything. This is actually a
rather simple-minded grid. I've just bound it to a binding source and
enabled adding. It's really mostly defaults aside from the DataError code
and a few values added in DefaultValuesNeeded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top