473,233 Members | 1,650 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,233 software developers and data experts.

Index -1 does not have a value - DataGridView

ME
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/forumarch...st23270599.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
Jan 11 '06 #1
1 12263
Hi,

"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:2r********************@comcast.com...
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);
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.
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/forumarch...st23270599.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

Jan 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DraguVaso | last post by:
Hi, I'm using the DataGridView in VB.NET 2.0. The DataSource is a Generic List of a custom class0: lstMyPersonnes = New List(Of clsPersonne). When I add a new clsPersonne to lstMyPersonnes,...
7
by: Robert Koller | last post by:
Hello i have this problem: In a DataGridView the user work with data from a DataSet. on Start the row index from datagridview is the same as the row index from the dataset. Also: ...
2
by: inpuarg | last post by:
Is it possible that - or is there any workarround for adding a new unbound row to a datagridview at bound mode ? Theese are not working. And i don 't want to add a row to dataset then rebind -...
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
5
by: Kathy | last post by:
I am trying to set a tooltip for a column cell in a data grid as documented in the MS Visual Studio 2005 documentation. I set up a test to match the example exactly including database column, data...
1
by: Andy | last post by:
Hi I have an app with an datagrid that I will replace with a datagridview controll. In the old datagrid I could use datagrid.currentrowIndex to get the index value for the current datarow,...
1
by: =?Utf-8?B?QnJpYW5ESA==?= | last post by:
Hi I am looking for examples of how to find the row index in a Datagridview control by comparing a string value to the value in a Cell. Thanks Brian
2
by: clogg02 | last post by:
Hi, I have bound a DataView to a DataGridView using this.dataGridView1.DataSource = new DataView(this.dataTable1); I allow for sorting of the datagridview using clicks on the headers. Later...
1
by: PIERREWHY | last post by:
I have a datagridview containing a datagridviewcombobox. The combobox has a dataset containing city_name and city_id. When I select a new city_name in the combobox I would like to return the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.