Skip to main content

BaseView.InvalidValueException Event

Enables you to provide a proper response to entering an invalid cell value.

Namespace: DevExpress.XtraGrid.Views.Base

Assembly: DevExpress.XtraGrid.v24.1.dll

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

Declaration

[DXCategory("Action")]
public event InvalidValueExceptionEventHandler InvalidValueException

Event Data

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

Property Description
ErrorText Gets or sets the error description to be displayed in the message box/tooltip. Inherited from ExceptionEventArgs.
Exception Gets the exception that caused the event. Inherited from ExceptionEventArgs.
ExceptionMode Gets or sets the type of response to supplying invalid values. Inherited from ExceptionEventArgs.
Value Gets an invalid value that caused the exception.
WindowCaption Gets or sets the caption of the error message box. Inherited from ExceptionEventArgs.

Remarks

The InvalidValueException event allows you to handle exceptions raised as the result of assigning invalid values to cells. Generally, this takes place when using a wrong value type or when raising an exception during validation (using the BaseView.ValidateEditor method). Write a handler for this event to perform the appropriate exception handling, as shown in the example below. The event parameter provides a number of properties that allow you to access exception information, specify whether the exception should be raised, etc.

For detailed information on how to handle exceptions, see the Validating Editors topic.

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