470,855 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

DataGridView binding problem with custom collection

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.

Jul 23 '06 #1
4 5700
"Stuart" <St****@discussions.microsoft.coma écrit dans le message de news:
B2**********************************@microsoft.com...

|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<YourClassto it if necessary. This then provides the required
functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 23 '06 #2
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.

"Joanna Carter [TeamB]" wrote:
"Stuart" <St****@discussions.microsoft.coma écrit dans le message de news:
B2**********************************@microsoft.com...

|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<YourClassto it if necessary. This then provides the required
functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 23 '06 #3
"Stuart" <St****@discussions.microsoft.coma écrit dans le message de news:
18**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 23 '06 #4
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.

"Joanna Carter [TeamB]" wrote:
"Stuart" <St****@discussions.microsoft.coma écrit dans le message de news:
18**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mitchell S. Honnert | last post: by
2 posts views Thread by michael sorens | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.