From 905351c57ab046ee7982f14690394b0978a168ba Mon Sep 17 00:00:00 2001 From: Ivan Zlatev Date: Mon, 26 Apr 2010 21:57:47 +0000 Subject: [PATCH] 2010-04-26 Ivan Zlatev * DataGridView.cs: Handle all possible cases for when a new column is added and there are existing rows with existing cells both in the non-databound and the databound scenario. [Fixes bug #583387] svn path=/trunk/mcs/; revision=156146 --- .../System.Windows.Forms/ChangeLog | 7 +++++++ .../System.Windows.Forms/DataGridView.cs | 11 +++++++++-- .../DataGridViewCellCollection.cs | 10 +++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog index 312c9517e88..4696ef75d30 100644 --- a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog +++ b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog @@ -1,3 +1,10 @@ +2010-04-26 Ivan Zlatev + + * DataGridView.cs: Handle all possible cases for when a new column is + added and there are existing rows with existing cells both in the + non-databound and the databound scenario. + [Fixes bug #583387] + 2010-04-26 Carlos Alberto Cortez * XplatUIX11.cs: When retrieving data from the x11 clipboard and we diff --git a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs index 9673ef84f64..6e811bfe84b 100644 --- a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs +++ b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs @@ -3795,8 +3795,15 @@ namespace System.Windows.Forms { if (!is_autogenerating_columns && columns.Count == 1) ReBind (); - foreach (DataGridViewRow row in Rows) - row.Cells.ReplaceOrAdd (e.Column.Index, (DataGridViewCell)e.Column.CellTemplate.Clone ()); + foreach (DataGridViewRow row in Rows) { + DataGridViewCell newCell = (DataGridViewCell)e.Column.CellTemplate.Clone (); + if (row.Cells.Count == columns.Count) + row.Cells.Replace (e.Column.Index, newCell); + else if (e.Column.Index >= row.Cells.Count) + row.Cells.Add (newCell); + else + row.Cells.Insert (e.Column.Index, newCell); + } } e.Column.DataColumnIndex = FindDataColumnIndex (e.Column); diff --git a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCellCollection.cs b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCellCollection.cs index 66d01be8a6f..eacce0b0ef5 100644 --- a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCellCollection.cs +++ b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCellCollection.cs @@ -123,14 +123,10 @@ namespace System.Windows.Forms return result; } - internal void ReplaceOrAdd (int columnIndex, DataGridViewCell dataGridViewCell) + internal void Replace (int columnIndex, DataGridViewCell dataGridViewCell) { - if (columnIndex >= 0 && columnIndex < base.List.Count) { - RemoveAt (columnIndex); - Insert (columnIndex, dataGridViewCell); - } else { - Add (dataGridViewCell); - } + RemoveAt (columnIndex); + Insert (columnIndex, dataGridViewCell); } [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)] -- 2.25.1