Home All Groups Group Topic Archive Search About

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

Author
26 Oct 2007 6:13 PM
B. Chernick
(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?

Author
27 Oct 2007 5:32 PM
Cor Ligthert[MVP]
Hi,

I don't see the relation to dispose. (Which has nothing to do with removing
or whatever from a managed object).

However, you have to handle your datagrid by its bindingsource. As you did
not tell what it is, then it is difficult to help you. If it is a datatable,
then you can have a look at the rejectchanges.

http://msdn2.microsoft.com/en-us/library/system.data.datarow.rejectchanges(VS.71).aspx

Cor

Show quote
"B. Chernick" <BChern***@discussions.microsoft.com> schreef in bericht
news:2787CF30-E4CF-484E-A883-B0FF7087D082@microsoft.com...
> (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?
>
Author
28 Oct 2007 9:51 PM
B. Chernick
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



Show quote
"Cor Ligthert[MVP]" wrote:

> Hi,
>
> I don't see the relation to dispose. (Which has nothing to do with removing
> or whatever from a managed object).
>
> However, you have to handle your datagrid by its bindingsource. As you did
> not tell what it is, then it is difficult to help you. If it is a datatable,
> then you can have a look at the rejectchanges.
>
> http://msdn2.microsoft.com/en-us/library/system.data.datarow.rejectchanges(VS.71).aspx
>
> Cor
>
> "B. Chernick" <BChern***@discussions.microsoft.com> schreef in bericht
> news:2787CF30-E4CF-484E-A883-B0FF7087D082@microsoft.com...
> > (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?
> >
>
Author
29 Oct 2007 5:58 PM
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

AddThis Social Bookmark Button