Back to Devexpress

BaseView.ValidatingEditor Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-baseview-be0d6572.md

latest7.1 KB
Original Source

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.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Editor")]
public event BaseContainerValidateEditorEventHandler ValidatingEditor
vb
<DXCategory("Editor")>
Public Event ValidatingEditor As BaseContainerValidateEditorEventHandler

Event Data

The ValidatingEditor event's data class is BaseContainerValidateEditorEventArgs. The following properties provide information specific to this event:

PropertyDescription
ErrorTextGets or sets the error description.
ValidGets or sets whether the value is valid.
ValueGets 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.

  • BaseView.InvalidValueException

  • 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.

csharp
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();
}
vb
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns

Private Sub GridView1_ValidatingEditor(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles GridView1.ValidatingEditor
    Dim view As ColumnView = sender
    Dim column As GridColumn = If(TryCast(e, EditFormValidateEditorEventArgs)?.Column, view.FocusedColumn)
    If column.Name <> "colBudget" Then Exit Sub
    If (Convert.ToInt32(e.Value) < 0) Or (Convert.ToInt32(e.Value) > 1000000) Then
        e.Valid = False
    End If
End Sub

Private Sub GridView1_InvalidValueException(sender As Object, e As DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs) Handles GridView1.InvalidValueException
    Dim view As ColumnView = sender
    If view Is Nothing Then
        Return
    End If
    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()
End Sub

See Also

Edit Data. Create Cell Editors. Validate User Input

PostEditor()

ValidateEditor()

InvalidValueException

ValidateRow

EditingMode

BaseView Class

BaseView Members

DevExpress.XtraGrid.Views.Base Namespace