2010-04-26 Ivan Zlatev <ivan@ivanz.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridView.cs
index 9673ef84f64bafc77ded2e440d2c68bba961f549..6e811bfe84bbc930753ad9b6a8f889cfbb3f4a01 100644 (file)
@@ -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);