[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Problem with AddNew row in DataGridView

Jason Barnett

5/1/2008 2:19:00 PM

I created a custom class that implements IBindingList. I've tested the class
and all properties and methods function as I expect.

When I assign an object (of my IBindingList class) to the DataSource
property of a DataGridView, everything appears to work fine. Records appear
appropriately. I'm able to add a new record, edit an existing, and delete
and existing all fine. However, if I enter the last row (AddNew row), exit
the row (without making any edits), and re-enter the last row (AddNew row),
then I get an error in my code stating that "Operation is not valid due to
the current state of the object."

I also notice that when I enter the last row, a new row is added to my
underlying collection, but when I exit the row (without making any edits)
then the row is not removed even though it does not appear in the
DataGridView.

How do I get the DataGridView to notify my IBindingList class that the new
row has been canceled?

I posted to this newsgroup, because I didn't feel this was related to a
specific language. However, if there is a more appropriate newsgroup for
this discussion, please let me know.

For what it's worth, I'm working with a VB.NET application, using the v2.0
frameworks and Visual Studio 2005.
3 Answers

Marc Gravell

5/1/2008 2:34:00 PM

0

You need to implement ICancelAddNew; it isn't trivial (you need to track
the editing item index) - but there are plenty of examples when you know
what to use as the search term - i.e. ICancelAddNew

Marc

Marc Gravell

5/1/2008 2:42:00 PM

0

Also - for the record, there is BindingList<T> which provides some of
the core IBindingList functionity (including AddNew, CancelNew and
EndNew) - and you can override virtual methods to fill in the missing
bits (sort, etc), or implement the more complex IBindingListView if you
want, etc.

Of course, it is also perfectly valid to implement from scratch... it
just might be more work.

Marc

Marc Gravell

5/1/2008 2:43:00 PM

0

Sorry to blitz... one other thing... heed the note here:

http://msdn.microsoft.com/en-us/library/system.componentmodel.icancela...

You must ignore the calls if the index is wrong!