Geeks With Blogs
O(geek) Ryan Cain, running in time proportional to Geek.

In developing one of our new applications I ran into an interesting issue with the DataGridView, which contained a DropDown (ComboBox) column that was bound to a different BindingSource.  The basic issue is that the user would click a LinkButton and select a file.  That process would create a new instance of an object on the backend, which would be added to a list that was then bound to the DataGridView.  The drop down option on this new item would be left null however.  When the user would go to click the drop down to make a selection, they would receive the following exception:

System.IndexOutOfRangeException: Index -1 does not have a value. at
System.Windows.Forms.CurrencyManager.get_Item(Int32 index) at
(DataGridViewCellEventArgs e) at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean
validationFailureOccurred) at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean
throughMouseClick) at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean
isShiftDown, Boolean isControlDown) at System.Windows.Forms.DataGridView.OnCellMouseDown
(DataGridViewCellMouseEventArgs e) at System.Windows.Forms.DataGridView.OnMouseDown
(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button,
Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.DataGridView.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at
System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This really didn't seem to make any sense.  We had done this many times before and it worked perfectly fine, it was just this specific code that was causing an issue.  The code at issue was the following:

recruitmentBindingSource.DataSource = recruitments;

dataGridViewAttachments.DataSource = null;
dataGridViewAttachments.DataSource = attachments;

The recruitmentBindingSource was used for the drop down, and the gridview was where our problem was.  Well it turns out the 2nd line where we set the DataSource of the gridview to null was the source of the exception.  We had placed that line in there to force the gridview to refresh its display data.  We were updating that attachments list in a service, and the one way to make the gridview refresh is to set its DataSource to null, then to the object you want to work with.  Well this appears to cause more problems than its worth!  (Especially since I closed another issue today that was being caused by this null "refresh hack.") 

After doing a little research I came across the following code, namely the ResetBindings(), which appears to be just what the doctor ordered in this case.  I hadn't used this method again, since I ran into issues with it a few months back on non-datagridview controls, but for gridviews that call seems to refresh the data nicely and force the grid to reread the data in the object collection.  The new code that resolves this issue now looks like:

recruitmentBindingSource.DataSource = recruitments;

attachmentBindingSource.DataSource = attachments;
dataGridViewAttachments.DataSource = attachmentBindingSource;

Notice I've changed the gridview to now use a BindingSource rather than directly binding to my list collection. 

Posted on Thursday, June 26, 2008 3:57 PM | Back to top

Comments on this post: DataGridView DropDown Column Index -1 Exception

# re: DataGridView DropDown Column Index -1 Exception
Requesting Gravatar...
it works. cheers man
Left by Asif on Jan 16, 2009 9:02 AM

# re: DataGridView DropDown Column Index -1 Exception
Requesting Gravatar...
Many thanks Ryan :-)
Left by Keith Hutchison on Apr 09, 2011 8:08 AM

# re: DataGridView DropDown Column Index -1 Exception
Requesting Gravatar...
WOW how did you find that!....Thanks
Left by Kerry on Mar 28, 2012 8:02 PM

Your comment:
 (will show your gravatar)

Copyright © Ryan Cain | Powered by: