Bug 24372 - ThrowNewArgumentOutOfRangeException when DataGridViewRowCollection Clear()/Remove() called
Summary: ThrowNewArgumentOutOfRangeException when DataGridViewRowCollection Clear()/Re...
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-11-09 23:51 UTC by Jim Westfall
Modified: 2014-11-26 13:02 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

code to reproduce the exception (983 bytes, application/octet-stream)
2014-11-09 23:51 UTC, Jim Westfall
same crash but using remove instead (971 bytes, application/octet-stream)
2014-11-10 22:11 UTC, Jim Westfall

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jim Westfall 2014-11-09 23:51:38 UTC
Created attachment 8674 [details]
code to reproduce the exception


System.ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
  at System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) [0x00000] in <filename unknown>:0 
  at System.Collections.ArrayList.get_Item (Int32 index) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridViewRowCollection.SharedRow (Int32 rowIndex) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.GetRowInternal (Int32 rowIndex) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.OnPaint (System.Windows.Forms.PaintEventArgs e) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control.WmPaint (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.DataGridView.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] in <filename unknown>:0 
  at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] in <filename unknown>:0 

To reproduce build/run the attached code.  When the form loads there will be 20k rows added, scroll down to the bottom and double click one of the rows.  This will result in the above crash.

The underlying issue is when a DataGridViewRowCollection.Clear() is done there is nothing in the DataGridView class thats updating first_row_index's value.  When OnPaint() fires the follow code is causing the exception since its trying to access invalid Rows.

                        // Reset all not displayed rows to !Displayed
                        for (int i = 0; i < first_row_index; i++)
                                GetRowInternal (i).DisplayedInternal = false;

It looks like the fix should probably go in DataGridView.OnRowsPreRemovedInternal() but I'm not that familiar with the DataGridView code.

Comment 1 Jim Westfall 2014-11-10 22:11:05 UTC
Created attachment 8687 [details]
same crash but using remove instead

I was also able to reproduce this by doing a row Remove() also.  With this code, scroll down to the bottom.  Each time you double click it will remove a row, do this until there are no more visible rows.  The application will throw the same exception.
Comment 2 Jim Westfall 2014-11-10 22:33:11 UTC
I ended up poking this a bit more and submitted a pull request with the fix.

Comment 3 Miguel de Icaza [MSFT] 2014-11-26 13:02:27 UTC
Applied the patch