Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid binding to collectionbase implementation

Status
Not open for further replies.

JontyMC

Programmer
Nov 26, 2001
1,276
GB
I've created a collection of objects that implement a certain interface. I've then bound a datagrid to this collection, but when I try to edit the collection via the datagrid I get the following exception:
Code:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: Index was out of range.  Must be non-negative and less than the size of the collection.
   at System.Collections.CollectionBase.System.Collections.IList.get_Item(Int32 index)
   at System.Windows.Forms.CurrencyManager.CancelCurrentEdit()
   at System.Windows.Forms.DataGrid.HandleEndCurrentEdit()
   at System.Windows.Forms.DataGrid.OnLeave_Grid()
   at System.Windows.Forms.DataGrid.OnLeave(EventArgs e)
   at System.Windows.Forms.Control.NotifyLeave()
   at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
Any ideas why?

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
If nothing is selected in the datagrid, "I THINK" the SelectedIndex property is -1, which of course can not be the index of a collection, which must be zero or positive.

you will have to check to ignore accessing the collection when the selected index is negative or null.

Ive never tied a collection to a Datagrid but I had the exact same problem when tying it to a listbox. I'm assuming this is the same cause though.
 
Something is selected, but I solved the problem. I was using DataSource property at run-time, when I switched to SetDataBinding it was fine.

Thanks

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top