473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 5856
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ME | last post by:
I was running into a problem with the DataGridView while binding it to an object Collection. I got it working and I thought others might like to know how. -------------- Problem -------------...
1
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business...
2
by: Steve | last post by:
Hi- OK, I've got a DataGridView, I've created a BindingSource from one of my Business Entity object (based on generated classes from EntitySpaces) I've left the default column setup so that all...
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
2
by: michael sorens | last post by:
I have been trying to figure out how to use DataSets, BindingSources, DataGridViews, and XML together, but it is a challenge. I understand how to populate a DataGridView with XML basically as: ...
0
by: c_shah | last post by:
I have the controls on my form display the values of the currently selected row of the DataGridView. I am binding my DataGridView to a collection (Collection - which implements IBindingList...
4
by: Hexman | last post by:
Hello All, I'd like to find out the best way to add a cb column to a dgv and process efficiently. I see at least two ways of doing it. ------------------------------- 1) Add a cb to the dgv,...
3
by: rossmclean | last post by:
Hi, I am trying to add "x" number of DGV controls at run time. In my app the user selects a dir with a number of mdbs in it. They then select which dbs they want to run a common SQL against. ...
3
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.