DataGridView.ValidateCell Event

Occurs after a user changes a cell value in the in-place editor and attempts to select another cell.

Namespace: DevExpress.XamarinForms.DataGrid

Assembly: DevExpress.XamarinForms.Grid.dll

Declaration

public event DataGridValidationEventHandler ValidateCell

Event Data

The ValidateCell event handler receives an argument of the DataGridValidationEventArgs type. The following properties provide information specific to this event.

Property Description
ErrorContent Gets or sets the validation error description.
FieldName Gets the field name of the column that contains the cell whose value is being edited.
Item Gets an object that specifies a data source record to which the grid's row containing the currently edited cell corresponds.
NewValue Gets the editor's value.
OldValue Gets the cell's value before editing is started.
RowHandle Gets the handle of the row that contains the cell whose value is being edited.

Examples

This example shows how to validate a new cell value when a user modified it in an in-place cell editor and moves focus to another cell.

The DataGridView instance is bound to a collection of orders, and its Quantity column allows users to enter positive numbers only. If a new cell value does not pass validation, an error message is displayed and the user cannot move focus to another cell until the error is corrected.

Cell Validation

Follow the steps below to validate cell values:

  1. Specify Validation Rules
    Subscribe to the DataGridView.ValidateCell event, and implement validation logic in the event handler.
    Use the event parameter's OldValue and NewValue properties to get the cell's original and new value (the value that should be validated). The FieldName and RowHandle properties allow you to obtain the column and row that contain the cell whose value is being edited.
    If a cell value fails validation, set the ErrorContent property to the string that describes the validation error. A user cannot move focus to another cell until the error is fixed and the ErrorContent string is empty.
  2. Indicate Errors
    Handle the DataGridView.ValidationError event to provide a response to entering invalid data (for example, show validation error message to users). This event parameter's ValidationErrorEventArgs.ErrorContent property contains the error description assigned to the DataGridValidationEventArgs.ErrorContent property in the ValidateCell event hander.
<dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}" 
                  ValidateCell="Grid_ValidateCell"
                  ValidationError="Grid_ValidationError">
    <!-- ... -->
</dxg:DataGridView>
using DevExpress.XamarinForms.DataGrid;
// ...

private void Grid_ValidateCell(DataGridValidationEventArgs e) {
    if (e.FieldName == "Quantity" && e.NewValue is int) { 
        if ((int)e.NewValue <= 0) {
            e.ErrorContent = "The value must be positive.";
            return;
        }
    }
    if (e.FieldName == "Quantity" && e.NewValue is String) {
        if (string.IsNullOrEmpty((string)e.NewValue)) {
            e.ErrorContent = "The value is required.";
            return;
        }
        int result;
        if (!int.TryParse((string)e.NewValue, out result)) {
            e.ErrorContent = "Only numeric values are accepted.";
            return;
        }
    }
}

private void Grid_ValidationError(ValidationErrorEventArgs e) {
    DisplayAlert("Input Error", e.ErrorContent, "OK");
}
See Also