System.IndexOutOfRangeException: Index -1 does not have a value. I get this exception when I try to do this: products.Add(pdi); this.dgvOffer.DataSource = products;
products is an ArrayList of Product objects.
In my case I have a bit of complexity maybe not the best solution, but the one I thought of first. So here goes. I have an array of ArrayLists and each item in that array holds a list of Products.
I then have a combobox to select different items in the arraylist. When that happens I have a variable ArrayList products, which I give the value of the corresponding ArrayList item in the array. And then assign the products to the datasource of my datagridview. Like so:
dgvOffer.DataSource = null; dgvOffer.Update(); products = productsComplete[((ComboBox)sender).SelectedIndex]; dgvOffer.DataSource = products; dgvOffer.Update(); this error only comes after I make a new selection in the combobox. If I leave the combobox intact it gives no errors.
Hope this get's out of my head to the screen right. |
| Andri Wednesday, August 03, 2005 12:11 PM |
If you can provide a simple repro project and file a bug I'll check to see if this still repros. You can also try to download the June CTP and try on the latest bits.
-mark Program Manager Microsoft This post is provided "as-is" |
| Mark Rideout Wednesday, August 03, 2005 4:55 PM |
If you can provide a simple repro project and file a bug I'll check to see if this still repros. You can also try to download the June CTP and try on the latest bits.
-mark Program Manager Microsoft This post is provided "as-is" |
| Mark Rideout Wednesday, August 03, 2005 4:55 PM |
remove the Update call after DataSource = null --- there is nothing to update |
| jferrell1211 Wednesday, August 03, 2005 6:18 PM |
Thanks for your answers, I started to do a repro project and then reminded that I had similar problems before. And found out that this error is bound to the first value added to the datasource arraylist. So I tested to add one item in all arraylists and the error was gone.
Is there any special things you have look in when adding the first item in an empty list if you have bound it to a datagridview? |
| Andri Thursday, August 04, 2005 11:09 AM |
I have figured out a solution, if it's a good one don't know but it works. The problem was that if you do: datagridview.datasource = something;
and if that something is an empty arraylist then you get an error when you add your first item to that arraylist like so: datagridview.datasource = null; something.add( new Object(xxxxx) ); datagridview.datasource = something; // error comes in this line that index is -1... datagridview.update();
so my fix was to not bind the array's to the datagridview until they have at least one value in them. |
| Andri Thursday, August 04, 2005 11:27 AM |
ArrayList doesn't support change notification (it doesn't have events that notify clients when items are added/removed/changed). In general, Windows Forms does not support binding to dynamic (changing) lists that don't support change notification. Rather than use an ArrayList, try using BindingList<T> (in System.ComponentModel). BindingList<T> supports the IBindingList interface and will work correctly in the scenario above. If you use an ArrayList and are adding/removing items to the ArrayList while it is bound, you will find you run into other issues.
Joe Stegman The Windows Forms Team Microsoft Corp. This posting is provided "AS IS" with no warranties, and confers no rights. |
| Joe Stegman Thursday, August 04, 2005 1:59 PM |
Please use SuspendLayout(), ResumeLayout() methods of the control. |
| Uitumen Wednesday, July 23, 2008 6:50 AM |
I'm not sure but if you binding dgv to a view from database and you don't have method to call update you have to set propertie for dgv (allow user to add rows and delete rows and editing to false) this may help |
| amro nabil Thursday, October 08, 2009 7:47 AM |