[MWF] DataGridView: ensure first_row_index will be valid after row removal
authorJim Westfall <jwestfall@surrealistic.net>
Tue, 11 Nov 2014 03:20:56 +0000 (19:20 -0800)
committerJim Westfall <jwestfall@surrealistic.net>
Tue, 11 Nov 2014 03:20:56 +0000 (19:20 -0800)
DataGridView keeps track of the first visible row in first_row_index.
DataGridViewRowCollection will notify DataGridView its about to delete 1 or
more rows via OnRowsPreRemovedInternal().  When this happens, we need to
ensure first_row_index will still fall within the number of rows that
will remain after the row removal.

This fixes https://bugzilla.xamarin.com/show_bug.cgi?id=24372 which I
reported.

mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs

index 393d5cba9aebfd3d0da99fd14f52067b5b2f70e6..fc2138147672358021ebc33209fc5c5590bc84cf 100644 (file)
@@ -5133,7 +5133,14 @@ namespace System.Windows.Forms {
                                        SetSelectedRowCore (rowIndex, false);
                        }
 
-                       if (Rows.Count - e.RowCount <= 0) {
+                       int RowsLeft = Rows.Count - e.RowCount;
+                       if (RowsLeft < 0)
+                               RowsLeft = 0;
+
+                       if (first_row_index > RowsLeft)
+                               first_row_index = RowsLeft;
+
+                       if (RowsLeft == 0) {
                                MoveCurrentCell (-1, -1, true, false, false, true);
                                hover_cell = null;
                        } else if (Columns.Count == 0) {
@@ -5141,8 +5148,8 @@ namespace System.Windows.Forms {
                                hover_cell = null;
                        } else if (currentCell != null && currentCell.RowIndex == e.RowIndex) {
                                int nextRowIndex = e.RowIndex;
-                               if (nextRowIndex >= Rows.Count - e.RowCount)
-                                       nextRowIndex = Rows.Count - 1 - e.RowCount;
+                               if (nextRowIndex >= RowsLeft)
+                                       nextRowIndex = RowsLeft - 1;
                                MoveCurrentCell (currentCell != null ? currentCell.ColumnIndex : 0, nextRowIndex, 
                                                 true, false, false, true);
                                if (hover_cell != null && hover_cell.RowIndex >= e.RowIndex)