Skip to main content
A newer version of this page is available. .

BaseView.ValidatingEditor Event

Enables you to perform manual validation of cell values and editor values (within the Edit Form).

Namespace: DevExpress.XtraGrid.Views.Base

Assembly: DevExpress.XtraGrid.v18.1.dll

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

Depending on the GridOptionsBehavior.EditingMode property, data in the GridView can be edited in-place (directly within cells) or using an Edit Form (a form with standalone editors, which can be opened inline or as a separate modal window). Other Views support only in-place editing. Regardless of the data editing mode, you can handle the ValidatingEditor event to validate data within cells and within the Edit Form‘s standalone editors.

While handling the ValidatingEditor event, you may need to identify the current row and column. The row can be identified using the ColumnView.FocusedRowHandle parameter. The currently processed column is identified differently dependent on the data editing mode:

Editor validation takes place when attempting to save the edit value of a cell or Edit Form‘s standalone editor.

First, the automatic validation is performed. The editor determines whether it can accept the entered value. After the automatic validation has been performed, the ValidatingEditor event is raised. Handle this event to implement custom constraints on cell values.

If the entered value doesn’t match your custom criteria, set the event’s Valid parameter to false. You can obtain the value which is about to be posted using the Value parameter.

If the Valid parameter value has been set to false, the edit value is not saved and the editor retains focus so that the end-users can correct it. Additionally, the editor displays an error icon (ColumnView.SetColumnError_ErrorGlyph) and the tooltip contains text specified by the ErrorText parameter. Note that the Valid parameter can be initially set to false. This takes place if the editor fails automatic validation.

The default event handling result (displaying an error icon and tooltip leaving the editor focus unchanged) can be overridden by handling the BaseView.InvalidValueException event.

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();
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ValidatingEditor event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also