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

G

Guest

(Reposted from Winforms forum. Not sure where this belongs.)

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

The grid is bound to a filtered binding source. Add is enabled. Part of
the primary key is set by one of the cells in the grid which is in combobox
mode. My main concern is duplicate keys. The combobox cell is the only
problem. The other row fields are either hidden or easy-to-validate text
entry boxes (handled in RowValidating).

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/additions. 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 (I'm even sure 'new' is the right
term, but it is newly added. By the time it gets to DataError, the row's
IsNewRow property = False).

Any attempt to get rid of the new row by any means seems to get me into
some sort of infinite loop back to DataError, with errors like 'Index
<number> does not have a value'.

Am I even trying to handle the error in the right place?
 
G

Guest

My apologies but I am not absolutely sure I understand your response. The
grids involved are using BindingSources as their datasources if that's your
question. Also your reference is interesting and I'll have to see if I can
use it. (I'm afraid that I've only recently started using the datagridview.)


However in the meantime I have found what appears to be a working solution
to my problem. Let me know if this makes sense to you. (This seems to do
everything I want.) Thanks.

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
 
C

Cor Ligthert[MVP]

Hi,

I express did not talk about the bindingsource because of the fact that the
bindingsource itself has a datasource. (it is nothing more than an extra
connector with extras).

If you use a DataGridView or any other complex datacontrol, then mostly it
is the easiest way to work with the onderlying dataclass.

Now you are creating something, that maybe works (I never did it like that,
so I cannot have an opinion). However is in my idea non common and
therefore hard to maintain.

Cor
 

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