473,761 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collec tionProperty.Ad d(myBasicObject );

dgvGrid.DataSou rce = myObject.Collec tionProperty;
bsBindingSource .DataSource = myObject.Collec tionProperty;
dgvGrid.DataSou rce = 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(b ool) 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.Collec tionProperty;
dgvGrid.DataSou rce = 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 12315
Hi,

"ME" <tr*********@co mcast.netREMOVE THIS> wrote in message
news:2r******** ************@co mcast.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.Collec tionProperty.Ad d(myBasicObject );

dgvGrid.DataSou rce = myObject.Collec tionProperty;
bsBindingSource .DataSource = myObject.Collec tionProperty;
dgvGrid.DataSou rce = 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(b ool) 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<Cus tomer>) 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.Collec tionProperty;
dgvGrid.DataSou rce = 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
3623
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, and rebind the lstMyPersonnes to my DataGridView.dataSource, it shows the new record in the DataGridView as it should be. But when I click on a cell of that row, I suddenly got this error: "Index -1
7
8365
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: DataGridView.Rows(5) <=====> DataSet.Rows(5) .
2
8352
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 - cause i want to add this row to a specific location. and it does not match to sort order. Regards. and thanks. base.Rows.InsertCopy(base.CurrentRow.Index, 1); base.Rows.Insert(base.CurrentRow.Index, 1);
2
5019
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 column A binds to string A and column B binds to int B. When i start two threads, the datagridview able to show two rows and updated int B at run time. How? public class Foo { public Foo()
5
4879
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 grid column and data values. I also have a tooltip control on the form. If I comment out the 'if' test, it works fine, but uncommented, I get a NullReferenceException error on the Me.dataGridView1.Columns("Rating").Index reference. Here is...
1
1920
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, but I'm not able to find anything similar for the datagridview controll. Any ideas, anyone?
1
9170
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
15082
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 on I need to determine the content of any selected cell or row in that datagridview once a button has been pressed.
1
2719
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 city_id corresponding to that city_name. Code: //City ComboBox comboboxCity.DataPropertyName = "city_name"; comboboxCity.HeaderText = "City"; comboboxCity.DataSource = city_table;
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.