473,404 Members | 2,114 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,404 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 12283
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.