mobilecontrols-401328-xamarin-forms-data-grid-examples-input-validation.md
The DataGridView allows you to validate new values in in-place cell editors or edit form, indicate errors, and prevent saving invalid data to the data source.
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:
<dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}"
ValidateCell="Grid_ValidateCell"
ValidationError="Grid_ValidationError">
<!-- ... -->
</dxg:DataGridView>
using Xamarin.Forms;
using DevExpress.XamarinForms.DataGrid;
// ...
private void Grid_ValidateCell(object sender, DataGridValidationEventArgs 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");
}
This example shows how to validate cell values that users enter in the edit form.
You can verify each value after a user modifies it (in the default edit form only) or validate the entire form (default or custom) when a user closes it.
Use the EditFormPage.ValidateCell event that occurs after a user changes a cell value in the default edit form and moves focus from the editor.
In the event hander:
<dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}"
Tap="Grid_Tap">
<!-- ... -->
</dxg:DataGridView>
using System;
using Xamarin.Forms;
using DevExpress.XamarinForms.DataGrid;
// ...
private void Grid_Tap(object sender, DataGridGestureEventArgs e) {
if (e.Item != null) {
var editForm = new EditFormPage(grid, grid.GetItem(e.RowHandle));
editForm.ValidateCell += EditForm_ValidateCell;
Navigation.PushAsync(editForm);
}
}
private void EditForm_ValidateCell(object sender, DataGridValidationEventArgs e) {
if (e.FieldName == "Quantity" && (decimal)e.NewValue <= 0) {
e.ErrorContent = "The value must be positive.";
}
else if (e.FieldName == "Date" && (DateTime)e.NewValue > DateTime.Now.Date)
e.ErrorContent = "The date value cannot be in the future.";
}
Use the EditFormPage.ValidateForm event that occurs after a user changes the values and tries to close the edit form.
In the event hander:
<dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}"
Tap="Grid_Tap">
<!-- ... -->
</dxg:DataGridView>
using System;
using Xamarin.Forms;
using DevExpress.XamarinForms.DataGrid;
// ...
private void Grid_Tap(object sender, DataGridGestureEventArgs e) {
if (e.Item != null) {
var editForm = new EditFormPage(grid, grid.GetItem(e.RowHandle));
editForm.ValidateForm += EditForm_ValidateForm;
Navigation.PushAsync(editForm);
}
}
private void EditForm_ValidateForm(object sender, EditFormValidationEventArgs e) {
if ((decimal)e.Values["Quantity"] <= 0) {
e.Errors.Add("Quantity", "The value must be positive.");
}
if ((DateTime)e.Values["Date"] > DateTime.Now.Date)
e.Errors.Add("Date", "The date value cannot be in the future.");
}