2009-02-03 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridView.cs
index 67a2e5160835e385828e02b3ee116949676a3500..fa23e0b34d2b018c9c154cb590ee5f763c1cf06a 100644 (file)
@@ -680,13 +680,12 @@ namespace System.Windows.Forms {
                                /// to the data cache, or the new cell is in a hidden
                                /// row.
                                /////////////////////////////////////////////////////
-                               if (value.DataGridView != this)
+                               if (value == null)
+                                       MoveCurrentCell (-1, -1, true, false, false, true);
+                               else if (value.DataGridView != this)
                                        throw new ArgumentException("The cell is not in this DataGridView.");
-
-                               if (value != null)
-                                       MoveCurrentCell (value.OwningColumn.Index, value.OwningRow.Index, true, false, false, true);
                                else
-                                       MoveCurrentCell (-1, -1, true, false, false, true);
+                                       MoveCurrentCell (value.OwningColumn.Index, value.OwningRow.Index, true, false, false, true);
                        }
                }
 
@@ -732,6 +731,7 @@ namespace System.Windows.Forms {
                                if (!(value == null || value is IList || value is IListSource || value is IBindingList || value is IBindingListView))
                                        throw new NotSupportedException ("Type cannot be bound.");
                                        
+                               ClearBinding ();
                                dataSource = value;
                                ReBind ();
                                OnDataSourceChanged (EventArgs.Empty);
@@ -2295,7 +2295,8 @@ namespace System.Windows.Forms {
                                cell.PositionEditingControl (true, true, this.GetCellDisplayRectangle (cell.ColumnIndex, cell.RowIndex, false), bounds, style, false, false, (columns [cell.ColumnIndex].DisplayIndex == 0), (cell.RowIndex == 0));
 
                                // Show the editing control
-                               EditingControlInternal.Visible = true;
+                               if (EditingControlInternal != null)
+                                       EditingControlInternal.Visible = true;
 
                                IDataGridViewEditingControl dgvEditingControl = (IDataGridViewEditingControl) EditingControlInternal;
                                if (dgvEditingControl != null) {
@@ -2328,7 +2329,8 @@ namespace System.Windows.Forms {
                                                DataManager.CancelCurrentEdit ();
 
                                        new_row_editing = false;
-                                       PrepareEditingRow (true, false);
+                                       PrepareEditingRow (false, false);
+                                       MoveCurrentCell (currentCell.ColumnIndex, NewRowIndex, true, false, false, true);
                                        OnUserDeletedRow (new DataGridViewRowEventArgs (EditingRow));
                                }
                        }
@@ -2427,7 +2429,9 @@ namespace System.Windows.Forms {
                        Focus ();
                        if (currentCell.RowIndex == NewRowIndex) {
                                new_row_editing = false;
-                               PrepareEditingRow (false, false);
+                               editing_row = null; // editing row becomes a real row
+                               PrepareEditingRow (true, false); // add a new editing row
+                               MoveCurrentCell (currentCell.ColumnIndex, NewRowIndex, true, false, false, true);
                        }
                        return true;
                }
@@ -3662,7 +3666,6 @@ namespace System.Windows.Forms {
                                //
                                if (!is_autogenerating_columns && columns.Count == 1)
                                        ReBind ();
-
                                foreach (DataGridViewRow row in Rows)
                                        row.Cells.Add ((DataGridViewCell)e.Column.CellTemplate.Clone ());
                        }
@@ -4825,6 +4828,9 @@ namespace System.Windows.Forms {
 
                internal void OnRowsAddedInternal (DataGridViewRowsAddedEventArgs e)
                {
+                       if (hover_cell != null && hover_cell.RowIndex >= e.RowIndex)
+                               hover_cell = null;
+
                        AutoResizeColumnsInternal ();
                        Invalidate ();
                        OnRowsAdded (e);
@@ -4851,14 +4857,18 @@ namespace System.Windows.Forms {
 
                        if (Rows.Count == 0) {
                                MoveCurrentCell (-1, -1, true, false, false, true);
+                               hover_cell = null;
                        } else if (Columns.Count == 0) {
                                MoveCurrentCell (-1, -1, true, false, false, true);
+                               hover_cell = null;
                        } else {
                                int nextRowIndex = e.RowIndex;
                                if (nextRowIndex >= Rows.Count)
                                        nextRowIndex = Rows.Count - 1;
                                MoveCurrentCell (currentCell != null ? currentCell.ColumnIndex : 0, nextRowIndex, 
                                                 true, false, false, true);
+                               if (hover_cell != null && hover_cell.RowIndex >= e.RowIndex)
+                                       hover_cell = null;
                        }
 
                        Invalidate ();
@@ -4920,15 +4930,17 @@ namespace System.Windows.Forms {
 
                protected virtual void OnUserAddedRow (DataGridViewRowEventArgs e)
                {
-                       // Switch the current editing row with a real
-                       int newRowIndex = NewRowIndex;
-                       int currentColumnIndex = currentCell != null ? currentCell.ColumnIndex : 0;
-                       new_row_editing = true;
                        PrepareEditingRow (false, false);
-                       if (DataManager != null)
+
+                       new_row_editing = true;
+                       if (DataManager != null) {
+                               // Switch the current editing row with a real one
+                               if (editing_row != null) {
+                                       Rows.RemoveInternal (editing_row);
+                                       editing_row = null;
+                               }
                                DataManager.AddNew (); // will raise OnListPositionChanged
-                       else
-                               MoveCurrentCell (currentColumnIndex, NewRowIndex, true, false, false, true);
+                       }
 
                        e = new DataGridViewRowEventArgs (Rows[NewRowIndex]);
                        DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserAddedRowEvent]);
@@ -5172,7 +5184,7 @@ namespace System.Windows.Forms {
                        if (cell != null) {
                                if (cell.KeyEntersEditMode (new KeyEventArgs ((Keys)m.WParam.ToInt32 ())))
                                        BeginEdit (true);
-                               if (EditingControl != null && (Msg)m.Msg == Msg.WM_KEYDOWN || (Msg)m.Msg == Msg.WM_CHAR)
+                               if (EditingControl != null && ((Msg)m.Msg == Msg.WM_KEYDOWN || (Msg)m.Msg == Msg.WM_CHAR))
                                        XplatUI.SendMessage (EditingControl.Handle, (Msg)m.Msg, m.WParam, m.LParam);
                        }
 
@@ -5412,10 +5424,12 @@ namespace System.Windows.Forms {
 
                        if (cell != currentCell) {
                                if (currentCell != null) {
-                                       if (currentCell.IsInEditMode && !EndEdit ())
-                                               return false;
-                                       else if (currentCell.RowIndex == NewRowIndex && new_row_editing)
-                                               CancelEdit ();
+                                       if (currentCell.IsInEditMode) {
+                                               if (!EndEdit ())
+                                                       return false;
+                                               else if (currentCell.RowIndex == NewRowIndex && new_row_editing)
+                                                       CancelEdit ();
+                                       }
                                        OnCellLeave (new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
                                        OnRowLeave (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
                                }
@@ -5774,30 +5788,23 @@ namespace System.Windows.Forms {
                        return bounds;
                }
 
-               internal void PrepareEditingRow (bool cell_changed, bool column_changed)
+               private void PrepareEditingRow (bool cell_changed, bool column_changed)
                {
-                       if (new_row_editing) {
-                               if (editing_row != null) {
-                                       Rows.RemoveInternal (editing_row);
-                                       editing_row = null;
-                               }
+                       if (new_row_editing)
                                return;
-                       }
 
                        bool show = false;
                        
                        show = ColumnCount > 0 && AllowUserToAddRows;
 
-                       if (!show && editing_row != null) {
-                               Rows.RemoveInternal (editing_row);
-                               editing_row = null;
+                       if (!show) {
+                               RemoveEditingRow ();
                        } else if (show) {
                                if (editing_row != null && (cell_changed || column_changed)) {
                                        // The row changed, it's no longer an editing row.
                                        //    or
                                        // The number of columns has changed, we need a new editing row.
-                                       Rows.RemoveInternal (editing_row);
-                                       editing_row = null;
+                                       RemoveEditingRow ();
                                }
                                if (editing_row == null) {
                                        editing_row = RowTemplateFull;
@@ -5806,6 +5813,15 @@ namespace System.Windows.Forms {
                        }
                }
                
+               internal void RemoveEditingRow ()
+               {
+                       if (editing_row != null) {
+                               if (Rows.Contains (editing_row))
+                                   Rows.RemoveInternal (editing_row);
+                               editing_row = null;
+                       }
+               }
+
                internal DataGridViewRow EditingRow {
                        get { return editing_row; }
                }
@@ -5850,18 +5866,20 @@ namespace System.Windows.Forms {
                {
                        if (type == typeof (bool))
                                return new DataGridViewCheckBoxColumn ();
+                       else if (typeof(Bitmap).IsAssignableFrom (type))
+                               return new DataGridViewImageColumn ();
                                
                        return new DataGridViewTextBoxColumn ();
                }
                
                private void ClearBinding ()
                {
-                       columns.ClearAutoGeneratedColumns ();
-                       rows.Clear ();
-                       PrepareEditingRow (false, true);
                        if (DataManager != null) {
                                DataManager.ListChanged -= OnListChanged;
                                DataManager.PositionChanged -= OnListPositionChanged;
+                               columns.ClearAutoGeneratedColumns ();
+                               rows.Clear ();
+                               RemoveEditingRow ();
                        }
                }
                
@@ -5919,6 +5937,7 @@ namespace System.Windows.Forms {
                                        MoveCurrentCell (0, 0, true, false, false, false);
                        }
 
+                       PrepareEditingRow (false, true);
                        PerformLayout();
                        Invalidate ();
                }