Home All Groups Group Topic Archive Search About

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

Author
26 Oct 2007 3:29 PM
B. Chernick
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.

Author
28 Oct 2007 1:32 PM
Brendon Bezuidenhout
Afternoon Chernick,

You were right on the money with RowValidating in my opinion... RowError
will get called if there are any errors in your row just being added to the
grid by the user. But before this happens the row needs to get validated
first.

Have a look here:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating.aspx

Regards,
Brendon

Show quote
"B. Chernick" <BChern***@discussions.microsoft.com> wrote in message
news:3B5A62FF-ABFE-4A8D-8727-61992EDB385A@microsoft.com...
> 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.
Author
28 Oct 2007 9:42 PM
B. Chernick
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

Show quote
"Brendon Bezuidenhout" wrote:

> Afternoon Chernick,
>
> You were right on the money with RowValidating in my opinion... RowError
> will get called if there are any errors in your row just being added to the
> grid by the user. But before this happens the row needs to get validated
> first.
>
> Have a look here:
> http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating.aspx
>
> Regards,
> Brendon
>
> "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message
> news:3B5A62FF-ABFE-4A8D-8727-61992EDB385A@microsoft.com...
> > 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.
>
Author
28 Oct 2007 11:06 PM
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)"


Show quote
"B. Chernick" <BChern***@discussions.microsoft.com> wrote in message
news:536D3FB9-184E-4693-AB32-F84C8EE5E1F8@microsoft.com...
>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
>
> "Brendon Bezuidenhout" wrote:
>
>> Afternoon Chernick,
>>
>> You were right on the money with RowValidating in my opinion... RowError
>> will get called if there are any errors in your row just being added to
>> the
>> grid by the user. But before this happens the row needs to get validated
>> first.
>>
>> Have a look here:
>> http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating.aspx
>>
>> Regards,
>> Brendon
>>
>> "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message
>> news:3B5A62FF-ABFE-4A8D-8727-61992EDB385A@microsoft.com...
>> > 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.
>>
Author
29 Oct 2007 1:42 PM
B. Chernick
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.

Show quote
"Brendon Bezuidenhout" wrote:

> 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)"
>
>
> "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message
> news:536D3FB9-184E-4693-AB32-F84C8EE5E1F8@microsoft.com...
> >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
> >
> > "Brendon Bezuidenhout" wrote:
> >
> >> Afternoon Chernick,
> >>
> >> You were right on the money with RowValidating in my opinion... RowError
> >> will get called if there are any errors in your row just being added to
> >> the
> >> grid by the user. But before this happens the row needs to get validated
> >> first.
> >>
> >> Have a look here:
> >> http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating.aspx
> >>
> >> Regards,
> >> Brendon
> >>
> >> "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message
> >> news:3B5A62FF-ABFE-4A8D-8727-61992EDB385A@microsoft.com...
> >> > 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.
> >>
>

AddThis Social Bookmark Button