maui-404487-data-grid-data-validation.md
The grid allows you to validate new values in in-place cell editors or edit forms, indicate errors, and prevent saving invalid data to the data source.
In in-place edit mode, you can handle the following events to validate user input.
DataGridView.ValidateCellOccurs after a user changes a cell value in the in-place editor and attempts to select another cell.DataGridView.ValidationErrorOccurs when a value in the in-place cell editor fails validation or when it cannot be saved to a data source.
For more information about in-place edit mode, refer to the following help topic: Edit Cell Values in In-Place Mode.
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.
Follow the steps below to validate cell values:
ErrorContent string is empty.<dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}"
ValidateCell="Grid_ValidateCell"
ValidationError="Grid_ValidationError">
<!-- ... -->
</dxg:DataGridView>
using Microsoft.Maui.Controls;
using DevExpress.Maui.DataGrid;
// ...
private void Grid_ValidateCell(object sender, ValidateCellEventArgs e) {
if (e.FieldName == "Quantity" && (decimal)e.NewValue <= 0)
e.ErrorContent = "The value must be positive.";
}
private void Grid_ValidationError(object sender, ValidationErrorEventArgs e) {
DisplayAlert("Input Error", e.ErrorContent, "OK");
}
Handle the ValidateAndSave event to validate data a user entered in CRUD forms. DataGridView runs validation mechanisms after a user taps the Save button in the CRUD form in a built-in detail form, or after you call the DetailEditFormViewModel.SaveAsync() method for a custom form.
If you use the DataFormView component to implement custom CRUD forms, you can use its validation mechanisms. They include data annotations, validation events, and error notification interfaces.
For more information about validation in CRUD forms, refer to the following help topic: Validate and Save Data