Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Index -1 does not have a value - DataGridView

8 views
Skip to first unread message

ME

unread,
Jan 10, 2006, 10:19:13 PM1/10/06
to
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 -------------
The grid was bound to a simple object collection. The object contained
string and decimal properties, nothing to fancy. When an item was added to
the collection I would databind as follows (this happened on each item
"Add"):

myObject.CollectionProperty.Add(myBasicObject);

dgvGrid.DataSource = myObject.CollectionProperty;
bsBindingSource.DataSource = myObject.CollectionProperty;
dgvGrid.DataSource = bsBindingSource;

The reason I was binding twice was that the first bind didn't appear to
allow the grid to hold more than one item.

Now at run time if I added an object using the method above the grid would
populate and it appeared to work fine. If I enabled deleting on the grid I
could even delete the objects (using the grid and the delete button on my
keyboard)... But wait! If I added an item after I deleted all the items in
the grid (collection) and try to select it the grid would bomb out with the
following error: "Index -1 does not have a value".

-------------- Solution -----------------
After searching endlessly for some info I found that a few folks (thomas &
Pieter) had run into this error but could not find any answer as to why. I
decided to examine related objects (looking for cool property or a method
that might save the day) and while doing so I came across the
ResetBindings(bool) method of the BindingSource.

I moved the binding information in the Add routine to a more appropriate
location in the form initialization Then I simply added

bsBindingSource.ResetBindings(false);

to the Add routine, just after the object is added to the collection.
Viola! Problem was fixed, and I no longer needed to bind the grid twice. I
was able to make the binding work now with just this:

bsBindingSource.DataSource = myObject.CollectionProperty;
dgvGrid.DataSource = bsBindingSource;

Any event that's probably just a problem I would have cause I'm just
amateur, but who knows, maybe it might help someone.

Thanks,

Matt

references:
http://www.dotnetnewsgroup.com/message/329608.aspx

http://www.eggheadcafe.com/forumarchives/netframeworknetwindowsforms/jul2005/post23270599.asp


Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: Professional

Microsoft Visual Basic 2005 77626-009-0000007-41501
Microsoft Visual Basic 2005

Microsoft Visual C# 2005 77626-009-0000007-41501
Microsoft Visual C# 2005

Microsoft Visual C++ 2005 77626-009-0000007-41501
Microsoft Visual C++ 2005

Microsoft Visual J# 2005 77626-009-0000007-41501
Microsoft Visual J# 2005

Microsoft Visual Web Developer 2005 77626-009-0000007-41501
Microsoft Visual Web Developer 2005

Crystal Reports AAC60-G0CSA4B-V7000AY
Crystal Reports for Visual Studio 2005


Bart Mermuys

unread,
Jan 11, 2006, 7:33:02 AM1/11/06
to
Hi,

"ME" <trash...@comcast.netREMOVETHIS> wrote in message
news:2rydnU-oz_W...@comcast.com...

If your custom collection is a BindingList<T> (eg. BindingList<Customer>) or
inherits from a BindingList<T> or implements IBindingList, then the
DataGridView will update itself when an object is added to the list.

But if it's neither, then Resetting the bindings seems like a good idea.

Greetings

0 new messages