Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
B. Chernick - 26 Oct 2007 16:29 GMT
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.
Brendon Bezuidenhout - 28 Oct 2007 14:32 GMT
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.rowva
lidating.aspx


Regards,
Brendon

> I'm writing a Winforms project in VB/Dot Net 2.0 and I'm having some
> trouble
[quoted text clipped - 18 lines]
> Am I even trying to handle the error in the right place?  RowValidating
> doesn't seem to do anything for index errors.
B. Chernick - 28 Oct 2007 22:42 GMT
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

> Afternoon Chernick,
>
[quoted text clipped - 31 lines]
> > Am I even trying to handle the error in the right place?  RowValidating
> > doesn't seem to do anything for index errors.
Brendon Bezuidenhout - 29 Oct 2007 00:06 GMT
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)"

>I may be missing something here.  The row does get validated as far as
> routine non-key fields are concerned.  No problem there.
[quoted text clipped - 69 lines]
>> > Am I even trying to handle the error in the right place?  RowValidating
>> > doesn't seem to do anything for index errors.
B. Chernick - 29 Oct 2007 14:42 GMT
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.

> Hmmm - With regards to the IndexOutOfRangeException and your BindingSource:
>
[quoted text clipped - 86 lines]
> >> > Am I even trying to handle the error in the right place?  RowValidating
> >> > doesn't seem to do anything for index errors.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.