BaseView.ValidatingEditor Event
Fires when a user edits a row cell value and attempts to select another cell. Handle this event to check whether this new value is valid, and if not, choose the required behavior (discard edits, show a warning message, ignore errors, or keep the focus on this cell until a user enters valid values). This event allows you to track edits made in the Edit Form.
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v24.1.dll
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
Declaration
[DXCategory("Editor")]
public event BaseContainerValidateEditorEventHandler ValidatingEditor
Event Data
The ValidatingEditor event's data class is BaseContainerValidateEditorEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
ErrorText | Gets or sets the error description. |
Valid | Gets or sets whether the value is valid. |
Value | Gets or sets the value being validated. |
Remarks
A cell is validated when a user has edited a value and presses Enter or moves focus to another cell within the same row. You can also forcibly trigger validation with the BaseView.PostEditor method. The diagram below illustrates the validation process.
BaseView.ValidatingEditor
.Handle this event to check whether or not a new cell value is correct and set the boolean e.Valid parameter accordingly. End-users are unable to leave a cell with an incorrect value until the error is fixed or the “Esc” key is pressed to undo changes.
BaseView.InvalidValueException
Raises if the e.Valid parameter of the previous event has been set to false and allows you to specify how the Data Grid responds to an invalid value. See the ExceptionMode enumerator values to see what options are available.
If the ExceptionMode parameter is set to NoAction, cells do not accept invalid values but do not notify users about errors. Use this approach if you want to implement custom notifications. For instance, you can provide default error icons for multiple columns at once with the ColumnView.SetColumnError method.
Tip
If users work with the Edit Form to edit data, cast the event parameter to the EditFormValidateEditorEventArgs class and read its e.Column property to obtain the current Grid column.
Online Video
WinForms Grid: Custom Value Entry.
Example
The following example prohibits invalid “colBudget” column cell value assignment. The cell value should be grater than zero and less than 1,000,000. The BaseView.ValidatingEditor
event is handled to check the entered value’s validity. The BaseView.InvalidValueException event is handled to display an exception message box if invalid cell value assignment occurs. In this instance, the GridView.HideEditor method is called to discard the changes made and to destroy the cell’s editor.
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
ColumnView view = sender as ColumnView;
GridColumn column = (e as EditFormValidateEditorEventArgs)?.Column ?? view.FocusedColumn;
if (column.Name != "colBudget") return;
if ((Convert.ToInt32(e.Value) < 0) || (Convert.ToInt32(e.Value) > 1000000))
e.Valid = false;
}
private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
ColumnView view = sender as ColumnView;
if (view == null) return;
e.ExceptionMode = ExceptionMode.DisplayError;
e.WindowCaption = "Input Error";
e.ErrorText = "The value should be greater than 0 and less than 1,000,000";
// Destroy the editor and discard the changes made within the edited cell.
view.HideEditor();
}