2008-12-08 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridView.cs
index ada8aef9c43b067a75735f9f3306a1ad6647a512..3f766d3eb01f01c462da2825d4cb66a9a6656b20 100644 (file)
@@ -2232,7 +2232,7 @@ namespace System.Windows.Forms {
                public virtual bool BeginEdit (bool selectAll) {
                        if (currentCell == null || currentCell.IsInEditMode)
                                return false;
-                       
+
                        if (currentCell.RowIndex >= 0) {
                                if ((currentCell.InheritedState & DataGridViewElementStates.ReadOnly) == DataGridViewElementStates.ReadOnly) {
                                        return false;
@@ -2291,6 +2291,7 @@ namespace System.Windows.Forms {
                                        dgvEditingControl.EditingControlRowIndex = currentCell.OwningRow.Index;
                                        dgvEditingControl.ApplyCellStyleToEditingControl (style);
                                        dgvEditingControl.PrepareEditingControlForEdit (selectAll);
+                                       dgvEditingControl.EditingControlFormattedValue = currentCell.EditedFormattedValue;
                                }
                                return true;
                        }
@@ -2336,14 +2337,29 @@ namespace System.Windows.Forms {
 
                public bool CommitEdit (DataGridViewDataErrorContexts context)
                {
-                       if (currentCell != null && currentCell.IsInEditMode) {
-                               IDataGridViewEditingControl ctrl = EditingControl as IDataGridViewEditingControl;
-                               ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
-                               currentCell.Value = ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
-                               return true;
+                       if (currentCell != null && currentCell.OwningRow.DataBoundItem != null) {
+                               Object ob = currentCell.OwningRow.DataBoundItem;
+                               PropertyDescriptor property = TypeDescriptor.GetProperties (ob)[currentCell.OwningColumn.DataPropertyName];
+                               if (property != null && !property.IsReadOnly) {
+                                       try {
+                                               object value = currentCell.Value;
+                                               if (property.Converter != null && 
+                                                   property.Converter.CanConvertFrom (value.GetType()))
+                                                       value = property.Converter.ConvertFrom (value);
+                                               property.SetValue (ob, value);
+                                               return true;
+                                       } catch (Exception exc) {
+                                               DataGridViewDataErrorEventArgs args = new DataGridViewDataErrorEventArgs (exc, currentCell.ColumnIndex, 
+                                                                                                                         currentCell.RowIndex, context);
+                                               InternalOnDataError (args);
+                                               if (args.ThrowException)
+                                                       throw exc;
+                                               return false;
+                                       }
+                               }
                        }
-                       
-                       return false;
+
+                       return true;
                }
 
                [MonoTODO ("Always includes partial columns")]
@@ -2385,31 +2401,34 @@ namespace System.Windows.Forms {
 
                public bool EndEdit ()
                {
-                       if (currentCell != null && currentCell.IsInEditMode) {
-                               if (EditingControl != null) {
-                                       IDataGridViewEditingControl ctrl = EditingControl as IDataGridViewEditingControl;
-                                       ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
-                                       currentCell.Value = ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
-                                       
-                                       currentCell.SetIsInEditMode (false);
-                                       currentCell.DetachEditingControl ();    
-                               } else if (currentCell is IDataGridViewEditingCell) {
-                                       currentCell.Value = (currentCell as IDataGridViewEditingCell).EditingCellFormattedValue;
-                                       currentCell.SetIsInEditMode (false);
-                               }
-
-                               new_row_commited = true;
-                               OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
-                       }
-                       
-                       Focus ();
-                       return true;
+                       return EndEdit (DataGridViewDataErrorContexts.Commit);
                }
 
                [MonoTODO ("Does not use context parameter")]
                public bool EndEdit (DataGridViewDataErrorContexts context)
                {
-                       return EndEdit ();
+                       if (currentCell == null || !currentCell.IsInEditMode)
+                               return true;
+
+                       if (EditingControl != null) {
+                               IDataGridViewEditingControl ctrl = EditingControl as IDataGridViewEditingControl;
+                               currentCell.Value = ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
+                               if (!CommitEdit (context)) {
+                                       EditingControl.Focus ();
+                                       return false;
+                               }
+                               currentCell.DetachEditingControl ();    
+                       } else if (currentCell is IDataGridViewEditingCell) {
+                               currentCell.Value = (currentCell as IDataGridViewEditingCell).EditingCellFormattedValue;
+                               if (!CommitEdit (context))
+                                       return false;
+                       }
+
+                       currentCell.SetIsInEditMode (false);
+                       new_row_commited = true;
+                       OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
+                       Focus ();
+                       return true;
                }
 
                public int GetCellCount (DataGridViewElementStates includeFilter) {
@@ -2847,7 +2866,8 @@ namespace System.Windows.Forms {
                }
 
                public virtual void NotifyCurrentCellDirty (bool dirty) {
-                       throw new NotImplementedException();
+                       if (currentCell != null)
+                               InvalidateCell (currentCell);
                }
 
                public bool RefreshEdit ()
@@ -4176,6 +4196,9 @@ namespace System.Windows.Forms {
                {
                        base.OnMouseDown(e);
                        
+                       if (!EndEdit ())
+                               return;
+
                        HitTestInfo hitTest = HitTest(e.X, e.Y);
                        
                        DataGridViewCell cell = null;
@@ -4184,7 +4207,6 @@ namespace System.Windows.Forms {
 
                        if (hitTest.Type == DataGridViewHitTestType.ColumnHeader && MouseOverColumnResize (hitTest.ColumnIndex, e.X)) {
                                if (e.Clicks == 2) {
-                                       EndEdit ();
                                        AutoResizeColumn (hitTest.ColumnIndex);
                                        return;
                                }
@@ -4193,14 +4215,12 @@ namespace System.Windows.Forms {
                                column_resize_active = true;
                                resize_band_start = e.X;
                                resize_band_delta = 0;
-                               EndEdit ();
                                DrawVerticalResizeLine (resize_band_start);
                                return;
                        }
 
                        if (hitTest.Type == DataGridViewHitTestType.RowHeader && MouseOverRowResize (hitTest.RowIndex, e.Y)) {
                                if (e.Clicks == 2) {
-                                       EndEdit ();
                                        AutoResizeRow (hitTest.RowIndex);
                                        return;
                                }
@@ -4209,7 +4229,6 @@ namespace System.Windows.Forms {
                                row_resize_active = true;
                                resize_band_start = e.Y;
                                resize_band_delta = 0;
-                               EndEdit ();
                                DrawHorizontalResizeLine (resize_band_start);
                                return;
                        }
@@ -5352,25 +5371,24 @@ namespace System.Windows.Forms {
                        if (cell != null && !cell.Visible)
                                throw new InvalidOperationException ("cell is not visible");
                                
-                       if (setAnchorCellAddress)
-                               anchor_cell = new Point (columnIndex, rowIndex);
-
                        if (cell != currentCell) {
                                if (currentCell != null) {
-                                       if (currentCell.IsInEditMode)
-                                               EndEdit ();
+                                       if (currentCell.IsInEditMode && !EndEdit ())
+                                               return false;
                                        OnCellLeave (new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
                                        OnRowLeave (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
                                }
 
                                currentCell = cell;
+                               if (setAnchorCellAddress)
+                                       anchor_cell = new Point (columnIndex, rowIndex);
                                currentCellAddress = new Point (columnIndex, rowIndex);
 
                                UpdateBindingPosition (currentCell.RowIndex);
                                OnRowEnter (new DataGridViewCellEventArgs (cell.ColumnIndex, cell.RowIndex));
                                OnCellEnter (new DataGridViewCellEventArgs(cell.ColumnIndex, cell.RowIndex));
                                OnCurrentCellChanged (EventArgs.Empty);
-                               if (throughMouseClick && editMode == DataGridViewEditMode.EditOnEnter)
+                               if (throughMouseClick || editMode == DataGridViewEditMode.EditOnEnter)
                                        BeginEdit (true);
                        } else {
                                if (throughMouseClick)
@@ -5944,7 +5962,8 @@ namespace System.Windows.Forms {
 
                private void MoveCurrentCell (int x, int y, bool select, bool isControl, bool isShift, bool scroll)
                {
-                       SetCurrentCellAddressCore (x, y, true, false, false);
+                       if (!SetCurrentCellAddressCore (x, y, true, false, false))
+                               return;
 
                        bool full_row_selected = Rows.SharedRow(CurrentCellAddress.Y).Selected;
                        bool full_col_selected = Columns[CurrentCellAddress.X].Selected;