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;
// Show the editing control
EditingControlInternal.Visible = true;
- // Allow editing control to set focus as needed
- (EditingControlInternal as IDataGridViewEditingControl).PrepareEditingControlForEdit (selectAll);
-
+ IDataGridViewEditingControl dgvEditingControl = (IDataGridViewEditingControl) EditingControlInternal;
+ if (dgvEditingControl != null) {
+ dgvEditingControl.EditingControlDataGridView = this;
+ dgvEditingControl.EditingControlRowIndex = currentCell.OwningRow.Index;
+ dgvEditingControl.ApplyCellStyleToEditingControl (style);
+ dgvEditingControl.PrepareEditingControlForEdit (selectAll);
+ dgvEditingControl.EditingControlFormattedValue = currentCell.EditedFormattedValue;
+ }
return true;
}
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")]
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) {
}
public virtual void NotifyCurrentCellDirty (bool dirty) {
- throw new NotImplementedException();
+ if (currentCell != null)
+ InvalidateCell (currentCell);
}
public bool RefreshEdit ()
{
base.OnMouseDown(e);
+ if (!EndEdit ())
+ return;
+
HitTestInfo hitTest = HitTest(e.X, e.Y);
DataGridViewCell cell = null;
if (hitTest.Type == DataGridViewHitTestType.ColumnHeader && MouseOverColumnResize (hitTest.ColumnIndex, e.X)) {
if (e.Clicks == 2) {
- EndEdit ();
AutoResizeColumn (hitTest.ColumnIndex);
return;
}
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;
}
row_resize_active = true;
resize_band_start = e.Y;
resize_band_delta = 0;
- EndEdit ();
DrawHorizontalResizeLine (resize_band_start);
return;
}
private void UpdateBindingPosition (int position)
{
- BindingSource source = dataSource as BindingSource;
- if (source != null && source.CurrencyManager != null)
- source.CurrencyManager.Position = position;
+ if (DataSource != null && BindingContext != null) {
+ CurrencyManager currencyManager = this.BindingContext[DataSource] as CurrencyManager;
+ if (currencyManager != null)
+ currencyManager.Position = position;
+ }
}
protected override void OnMouseEnter (EventArgs e)
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)
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;