From fb7332bec128c08c7a84a4fb14a97dc846b96d59 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexander=20K=C3=B6plinger?= Date: Thu, 27 Nov 2014 05:29:32 +0100 Subject: [PATCH] [MWF] Fix bug in DataGridViewRowCollection Clear() introduced by recent commit 7d1b878fbf13462a282860c6d1dd493a48d7b1eb altered the behavior of removing rows, breaking the DataGridViewRowCollectionTest.ClearRows () test Before the change, this is what happened when the list was cleared (the number in brackets is the value of the Index property of the DataGridViewRow): [0] Remove [1] ReIndex [0] Remove [1] ReIndex [0] [1] --------> [2] --------> [1] ------> [2] -------> [1] ... [2] [3] [2] [3] After the offending commit, ReIndex is only called after the list is cleared, which means the Index property of the rows is never updated (as there are no items in the list). The fix is to explicitly set the Index of each row to 0. This was the end result before the commit anyway, so we can omit the ReIndex call entirely. --- .../System.Windows.Forms/DataGridViewRowCollection.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewRowCollection.cs b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewRowCollection.cs index 9badc1dc7ab..24aa28797ba 100644 --- a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewRowCollection.cs +++ b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewRowCollection.cs @@ -261,7 +261,8 @@ namespace System.Windows.Forms for (int i = 0; i < total; i++) { DataGridViewRow row = (DataGridViewRow)list[0]; - + row.SetIndex(0); + // We can exit because the NewRow is always last if (row.IsNewRow) break; @@ -270,7 +271,6 @@ namespace System.Windows.Forms list.Remove (row); } - ReIndex (); DataGridView.OnRowsPostRemovedInternal (new DataGridViewRowsRemovedEventArgs (0, total)); OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null)); } -- 2.11.4.GIT