Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2008

Tip: Looking for answers? Try searching our database.

Index -1 does not have a value - DataGridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ME - 11 Jan 2006 03:19 GMT
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/pos
t23270599.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 - 11 Jan 2006 12:33 GMT
Hi,

>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
[quoted text clipped - 33 lines]
>
> bsBindingSource.ResetBindings(false);

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

> 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.
[quoted text clipped - 39 lines]
> Crystal Reports    AAC60-G0CSA4B-V7000AY
> Crystal Reports for Visual Studio 2005
krishna_vm - 02 Aug 2008 14:42 GMT
Hi Matt,

This helped me a great when i was using same kind of generic list binding
with datagridview in one of our applications.

Great Job!

Thanks a lot.

Regards
Gopal

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.