DataGridView binding problem with custom collection

G

Guest

I hope someone can shed some light on an error I've been experiencing for
sometime now, but can no longer continue to ignore :-(
I've created a custom entity class which implements IEditableObject. I have
then created a custom collection for this custom entity which naturally
inherits CollectionBase and implements IBindingList.

When I bind my custom collection to a DataGridView and started editing, it
works correctly only up to the point where you try to navigate off a newly
created (but empty) grid row - the second time, at which point it errors with
an ArgumentOutOfRange exception.

The stack trace indicates the error occurs in the GetCellDisplayRectangle
event and that the parameter name (of the invalid parameter) is rowIndex.

I'll try to explain a little better. If you add a row, by typing in some
values, the grid puts that row in "edit" mode and displays a new, empty row
below. If you use the arrow keys to move the cursor onto that new row, then
off again (down, then back up) everything is okay, if you repeat this process
(arrow back down to the new, empty row and back up again) the error occurs.

I suppose this must have something to do with what I am, or am not doing
with the custom collection but I've just built a really simplified version of
this, with just a couple of properties in the custom entity and no code other
than the basics in the collection (add, remove elements etc) and the error
still occurs.

Any help would be greatly appreciated.

Thanks in advance.
 
J

Joanna Carter [TeamB]

"Stuart" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I hope someone can shed some light on an error I've been experiencing for
| sometime now, but can no longer continue to ignore :-(
| I've created a custom entity class which implements IEditableObject. I
have
| then created a custom collection for this custom entity which naturally
| inherits CollectionBase and implements IBindingList.

You don't need to create your own custom collection classes, simply create
an instance of BindingList<YourClass>, passing an instance of
List<YourClass> to it if necessary. This then provides the required
functionality.

Joanna
 
G

Guest

Thanks for your input Joanna.

Originally I was using Generics as you suggest, however I am extending the
custom colleciton in other ways and therefore need to understand / figure out
why this isn't working.
 
J

Joanna Carter [TeamB]

"Stuart" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Originally I was using Generics as you suggest, however I am extending the
| custom colleciton in other ways and therefore need to understand / figure
out
| why this isn't working.

In that case, does your list class implement ICancelAddNew ? This is
important and, if it is not there, sounds like it could be the cause of your
problem

Joanna
 
G

Guest

That's it!

Thankyou so much Joanna.

I implemented the ICancelAddNew, and found I had to remove the item using
the itemIndex parameter (this.RemoveAt(itemIndex) ) in the CancelNew
funciton, and it works. (This itemIndex was increasing each time an item was
added, so the second time, the underlying list, seemed to have more items
than the DataGridView it was bound to...)

Thanks again Joanna.

I am off to investigate the BindingList<> "generics" solution you mentioned
as well. As I mentioned, I was using the standard List<> generics approach
we had a couple of limitations with regards to business logic I wanted to
implement but the BindingList approach may well be more flexible in this
regard.

Thanks.
 

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