private bool EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, bool fireCellLeave, bool fireCellEnter, bool fireRowLeave, bool fireRowEnter, bool fireLeave, bool keepFocus, bool resetCurrentCell, bool resetAnchorCell) { if (this.ptCurrentCell.X == -1) { return true; } this.dataGridViewOper[DATAGRIDVIEWOPER_inEndEdit] = true; try { int curRowIndex = this.ptCurrentCell.Y; int curColIndex = this.ptCurrentCell.X; DataGridViewCell dataGridViewCurrentCell = this.CurrentCellInternal; DataGridViewDataErrorEventArgs dgvdee = CommitEdit(ref dataGridViewCurrentCell, context, validateCell, fireCellLeave, fireCellEnter, fireRowLeave, fireRowEnter, fireLeave); if (dgvdee != null) { if (dgvdee.ThrowException) { throw dgvdee.Exception; } if (dgvdee.Cancel) { return false; } dgvdee = CancelEditPrivate(/*ref dataGridViewCurrentCell, context*/); // restore old value if (null != dgvdee) { if (dgvdee.ThrowException) { throw dgvdee.Exception; } if (dgvdee.Cancel) { return false; } } } if (!this.IsCurrentCellInEditMode) { return true; } if (curRowIndex != this.ptCurrentCell.Y || curColIndex != this.ptCurrentCell.X) { return true; } if (this.editingControl != null) { UnwireEditingControlEvents(); this.dataGridViewState2[DATAGRIDVIEWSTATE2_mouseOverRemovedEditingCtrl] = this.MouseOverEditingControl; this.dataGridViewState2[DATAGRIDVIEWSTATE2_mouseOverRemovedEditingPanel] = this.MouseOverEditingPanel; this.dataGridViewState1[DATAGRIDVIEWSTATE1_editingControlChanging] = true; try { dataGridViewCurrentCell.DetachEditingControl(); } finally { this.dataGridViewState1[DATAGRIDVIEWSTATE1_editingControlChanging] = false; } ImeMode editingControlImeMode = this.editingControl.CachedImeMode; // If in restricted mode, ImeMode will be Disable. this.latestEditingControl = this.editingControl; Debug.Assert(this.editingPanel == null || this.editingPanel.Controls.Count == 0); this.editingControl = null; InvalidateCellPrivate(this.ptCurrentCell.X, this.ptCurrentCell.Y); if (this.EditMode == DataGridViewEditMode.EditOnEnter) { if (resetCurrentCell) { bool success = SetCurrentCellAddressCore(-1, -1, resetAnchorCell, false, false); Debug.Assert(success); } } if (keepFocus) { // Debug.Assert(this.CanFocus || this.Focused); // Invalid assertion (VS Whidbey 325154) FocusInternal(); } this.ImeMode = editingControlImeMode; } else { Debug.Assert(this.dataGridViewState1[DATAGRIDVIEWSTATE1_currentCellInEditMode]); this.dataGridViewState1[DATAGRIDVIEWSTATE1_currentCellInEditMode] = false; InvalidateCellPrivate(this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (!IsInnerCellOutOfBounds(curColIndex, curRowIndex)) { DataGridViewCellEventArgs dgvce = new DataGridViewCellEventArgs(curColIndex, curRowIndex); OnCellEndEdit(dgvce); } return true; } finally { this.dataGridViewOper[DATAGRIDVIEWOPER_inEndEdit] = false; } }
private bool EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, bool fireCellLeave, bool fireCellEnter, bool fireRowLeave, bool fireRowEnter, bool fireLeave, bool keepFocus, bool resetCurrentCell, bool resetAnchorCell) { bool flag; if (this.ptCurrentCell.X == -1) { return true; } this.dataGridViewOper[0x400000] = true; try { int y = this.ptCurrentCell.Y; int x = this.ptCurrentCell.X; DataGridViewCell currentCellInternal = this.CurrentCellInternal; DataGridViewDataErrorEventArgs args = this.CommitEdit(ref currentCellInternal, context, validateCell, fireCellLeave, fireCellEnter, fireRowLeave, fireRowEnter, fireLeave); if (args != null) { if (args.ThrowException) { throw args.Exception; } if (args.Cancel) { return false; } args = this.CancelEditPrivate(); if (args != null) { if (args.ThrowException) { throw args.Exception; } if (args.Cancel) { return false; } } } if (!this.IsCurrentCellInEditMode) { return true; } if ((y != this.ptCurrentCell.Y) || (x != this.ptCurrentCell.X)) { return true; } if (this.editingControl != null) { this.UnwireEditingControlEvents(); this.dataGridViewState2[8] = this.MouseOverEditingControl; this.dataGridViewState2[0x10] = this.MouseOverEditingPanel; this.dataGridViewState1[0x4000] = true; try { currentCellInternal.DetachEditingControl(); } finally { this.dataGridViewState1[0x4000] = false; } ImeMode cachedImeMode = this.editingControl.CachedImeMode; this.latestEditingControl = this.editingControl; this.editingControl = null; this.InvalidateCellPrivate(this.ptCurrentCell.X, this.ptCurrentCell.Y); if ((this.EditMode == DataGridViewEditMode.EditOnEnter) && resetCurrentCell) { this.SetCurrentCellAddressCore(-1, -1, resetAnchorCell, false, false); } if (keepFocus) { this.FocusInternal(); } base.ImeMode = cachedImeMode; } else { this.dataGridViewState1[0x8000] = false; this.InvalidateCellPrivate(this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (!this.IsInnerCellOutOfBounds(x, y)) { DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(x, y); this.OnCellEndEdit(e); } flag = true; } finally { this.dataGridViewOper[0x400000] = false; } return flag; }
private DataGridViewDataErrorEventArgs CommitEdit(ref DataGridViewCell dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, bool fireCellLeave, bool fireCellEnter, bool fireRowLeave, bool fireRowEnter, bool fireLeave) { if (validateCell == DataGridViewValidateCellInternal.Always) { Debug.Assert(this.ptCurrentCell.X > -1); if (fireCellLeave) { if (this.ptCurrentCell.X == -1) { return null; } OnCellLeave(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (fireRowLeave) { if (this.ptCurrentCell.X == -1) { return null; } OnRowLeave(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (fireLeave) { base.OnLeave(EventArgs.Empty); // [....]: can we be smarter about this? What if validating the current cell below forces a repaint on the cell? // we would end up repainting the current cell twice. // // invalidate the current cell so the data grid view does not paint the focus rectangle any longer if (this.ptCurrentCell.X > -1 && this.ptCurrentCell.Y > -1) { InvalidateCellPrivate(this.ptCurrentCell.X /*columnIndex*/, this.ptCurrentCell.Y /*rowIndex*/); } } // OnCellValidating returns true if the dev cancelled the validation. bool validateFormattedValue = CanValidateDataBoundDataGridViewCell(dataGridViewCurrentCell); if (validateFormattedValue) { if (this.ptCurrentCell.X == -1) { return null; } if (OnCellValidating(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y, context)) { if (fireRowEnter) { if (this.ptCurrentCell.X == -1) { return null; } OnRowEnter(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y, true /*canCreateNewRow*/, true /*validationFailureOccurred*/); } if (fireCellEnter) { if (this.ptCurrentCell.X == -1) { return null; } OnCellEnter(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (this.ptCurrentCell.X == -1) { return null; } DataGridViewDataErrorEventArgs dgvdee = new DataGridViewDataErrorEventArgs(null, this.ptCurrentCell.X, this.ptCurrentCell.Y, // null, // null, context); dgvdee.Cancel = true; return dgvdee; } if (!this.IsCurrentCellInEditMode || !this.IsCurrentCellDirty) { if (this.ptCurrentCell.X == -1) { return null; } OnCellValidated(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } } } if (this.ptCurrentCell.X == -1 || !this.IsCurrentCellInEditMode) { return null; } Debug.Assert( ( (this.editingControl != null && ((IDataGridViewEditingControl)this.editingControl).EditingControlValueChanged) || (this.dataGridViewState1[DATAGRIDVIEWSTATE1_currentCellInEditMode] && ((IDataGridViewEditingCell)this.CurrentCellInternal).EditingCellValueChanged) ) == this.IsCurrentCellDirty || this.dataGridViewState1[DATAGRIDVIEWSTATE1_ignoringEditingChanges]); if (this.IsCurrentCellDirty) { bool validateAndPushFormattedValue = CanValidateDataBoundDataGridViewCell(dataGridViewCurrentCell); if (validateAndPushFormattedValue) { if (validateCell == DataGridViewValidateCellInternal.WhenChanged) { Debug.Assert(this.ptCurrentCell.X > -1); if (this.ptCurrentCell.X == -1) { return null; } if (OnCellValidating(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y, context)) { if (this.ptCurrentCell.X == -1) { return null; } DataGridViewDataErrorEventArgs dgvdee = new DataGridViewDataErrorEventArgs(null, this.ptCurrentCell.X, this.ptCurrentCell.Y, context); dgvdee.Cancel = true; return dgvdee; } } Exception exception; object formattedValue; if (this.editingControl != null) { formattedValue = ((IDataGridViewEditingControl)this.editingControl).GetEditingControlFormattedValue(context); } else { Debug.Assert(this.dataGridViewState1[DATAGRIDVIEWSTATE1_currentCellInEditMode]); formattedValue = ((IDataGridViewEditingCell)this.CurrentCellInternal).GetEditingCellFormattedValue(context); } if (!PushFormattedValue(ref dataGridViewCurrentCell, formattedValue, out exception)) { if (this.ptCurrentCell.X == -1) { return null; } DataGridViewDataErrorEventArgs dgvdee = new DataGridViewDataErrorEventArgs(exception, this.ptCurrentCell.X, this.ptCurrentCell.Y, // dataGridViewCurrentCell.Value, // formattedValue, context); dgvdee.Cancel = true; OnDataErrorInternal(dgvdee); return dgvdee; } if (!this.IsCurrentCellInEditMode) { return null; } this.uneditedFormattedValue = formattedValue; } if (this.editingControl != null) { ((IDataGridViewEditingControl) this.editingControl).EditingControlValueChanged = false; } else { Debug.Assert(this.dataGridViewState1[DATAGRIDVIEWSTATE1_currentCellInEditMode]); ((IDataGridViewEditingCell) this.CurrentCellInternal).EditingCellValueChanged = false; } this.IsCurrentCellDirtyInternal = false; this.IsCurrentRowDirtyInternal = true; if (validateAndPushFormattedValue) { if (validateCell == DataGridViewValidateCellInternal.Always || validateCell == DataGridViewValidateCellInternal.WhenChanged) { if (this.ptCurrentCell.X == -1) { return null; } OnCellValidated(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } } } return null; }
private DataGridViewDataErrorEventArgs CommitEdit(ref DataGridViewCell dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, bool fireCellLeave, bool fireCellEnter, bool fireRowLeave, bool fireRowEnter, bool fireLeave) { if (validateCell == DataGridViewValidateCellInternal.Always) { if (fireCellLeave) { if (this.ptCurrentCell.X == -1) { return null; } this.OnCellLeave(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (fireRowLeave) { if (this.ptCurrentCell.X == -1) { return null; } this.OnRowLeave(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (fireLeave) { base.OnLeave(EventArgs.Empty); if ((this.ptCurrentCell.X > -1) && (this.ptCurrentCell.Y > -1)) { this.InvalidateCellPrivate(this.ptCurrentCell.X, this.ptCurrentCell.Y); } } if (this.CanValidateDataBoundDataGridViewCell(dataGridViewCurrentCell)) { if (this.ptCurrentCell.X == -1) { return null; } if (this.OnCellValidating(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y, context)) { if (fireRowEnter) { if (this.ptCurrentCell.X == -1) { return null; } this.OnRowEnter(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y, true, true); } if (fireCellEnter) { if (this.ptCurrentCell.X == -1) { return null; } this.OnCellEnter(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } if (this.ptCurrentCell.X == -1) { return null; } return new DataGridViewDataErrorEventArgs(null, this.ptCurrentCell.X, this.ptCurrentCell.Y, context) { Cancel = true }; } if (!this.IsCurrentCellInEditMode || !this.IsCurrentCellDirty) { if (this.ptCurrentCell.X == -1) { return null; } this.OnCellValidated(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } } } if (((this.ptCurrentCell.X != -1) && this.IsCurrentCellInEditMode) && this.IsCurrentCellDirty) { bool flag2 = this.CanValidateDataBoundDataGridViewCell(dataGridViewCurrentCell); if (flag2) { Exception exception; object editingControlFormattedValue; if (validateCell == DataGridViewValidateCellInternal.WhenChanged) { if (this.ptCurrentCell.X == -1) { return null; } if (this.OnCellValidating(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y, context)) { if (this.ptCurrentCell.X == -1) { return null; } return new DataGridViewDataErrorEventArgs(null, this.ptCurrentCell.X, this.ptCurrentCell.Y, context) { Cancel = true }; } } if (this.editingControl != null) { editingControlFormattedValue = ((IDataGridViewEditingControl) this.editingControl).GetEditingControlFormattedValue(context); } else { editingControlFormattedValue = ((IDataGridViewEditingCell) this.CurrentCellInternal).GetEditingCellFormattedValue(context); } if (!this.PushFormattedValue(ref dataGridViewCurrentCell, editingControlFormattedValue, out exception)) { if (this.ptCurrentCell.X == -1) { return null; } DataGridViewDataErrorEventArgs e = new DataGridViewDataErrorEventArgs(exception, this.ptCurrentCell.X, this.ptCurrentCell.Y, context) { Cancel = true }; this.OnDataErrorInternal(e); return e; } if (!this.IsCurrentCellInEditMode) { return null; } this.uneditedFormattedValue = editingControlFormattedValue; } if (this.editingControl != null) { ((IDataGridViewEditingControl) this.editingControl).EditingControlValueChanged = false; } else { ((IDataGridViewEditingCell) this.CurrentCellInternal).EditingCellValueChanged = false; } this.IsCurrentCellDirtyInternal = false; this.IsCurrentRowDirtyInternal = true; if (flag2 && ((validateCell == DataGridViewValidateCellInternal.Always) || (validateCell == DataGridViewValidateCellInternal.WhenChanged))) { if (this.ptCurrentCell.X == -1) { return null; } this.OnCellValidated(ref dataGridViewCurrentCell, this.ptCurrentCell.X, this.ptCurrentCell.Y); } } return null; }