Problem with AddNew row in DataGridView

J

Jason Barnett

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.
 
M

Marc Gravell

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
 
M

Marc Gravell

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
 

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