windowsforms-114741-controls-and-libraries-data-grid-getting-started-walkthroughs-data-editing-tutorial-data-input-validation.md
Oct 29, 2020
5 minutes to read
The grid control has built-in data type validation enabled by default. If you enter a string value into a column bound to a numeric data field, and then, press ENTER or try to move focus away from the cell, the grid validates data input and raises an error as the string cannot be converted to a numeric value. Hover over the error icon to see the same error message.
Correct the entered value or discard changes to continue working with the grid control. To cancel your changes, press ESCAPE.
To manually specify the default error message, do the following.
private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
GridView view = sender as GridView;
if (view.FocusedColumn.FieldName == "UnitPrice") {
double price = 0;
if (!Double.TryParse(e.Value as String, out price)) {
e.Valid = false;
e.ErrorText = "Only numeric values are accepted.";
}
}
}
Run the application to see how the validation mechanism now shows your custom-defined error message.
To validate data, do the following:
private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
GridView view = sender as GridView;
if (view.FocusedColumn.FieldName == "UnitPrice") {
double price = 0;
if (!Double.TryParse(e.Value as String, out price)) {
e.Valid = false;
e.ErrorText = "Only numeric values are accepted.";
}
else if (price <= 0) {
e.Valid = false;
e.ErrorText = "The unit price must be positive.";
}
}
}
If you run the application, you will see that the specified error message appears on an attempt to enter zero into a cell.
To customize how errors are displayed, do the following.
private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
MessageBox.Show(this, e.ErrorText, "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
e.ExceptionMode = ExceptionMode.NoAction;
}
The grid control now informs you about the error using the Message Box.
If a value’s validity depends on other values within the same row, you can enable row validation by handling the ColumnView.ValidateRow event.
For instance, make sure that values in one column are greater than values in another column.
private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
GridView view = sender as GridView;
GridColumn colUnitsInStock = view.Columns["UnitsInStock"];
GridColumn colUnitsOnOrder = view.Columns["UnitsOnOrder"];
double unitsInStock = Convert.ToDouble(view.GetRowCellValue(view.FocusedRowHandle, colUnitsInStock));
double unitsOnOrder = Convert.ToDouble(view.GetRowCellValue(view.FocusedRowHandle, colUnitsOnOrder));
if (unitsInStock < unitsOnOrder) {
view.SetColumnError(colUnitsInStock, "The Units On Order value should be less than this value.");
view.SetColumnError(colUnitsOnOrder, "This value should be less than the Units In Stock value.");
view.SetColumnError(null, "Invalid data");
e.Valid = false;
e.ErrorText = "'Units On Order' and 'Units In Stock' values are not consistent.";
}
}
Run the application, enter the invalid value and try to switch focus to another row. This is when row validation takes place and the default Error Dialog with the specified error text is invoked.
Click Yes and then hover over error icons to see error messages specified for individual columns.
To disable the default dialog window when validating rows, handle the ColumnView.InvalidRowException event and set the ExceptionEventArgs.ExceptionMode parameter to ExceptionMode.NoAction
private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
e.ExceptionMode = ExceptionMode.NoAction;
}
The message is now suppressed when entering incorrect values, but you can still see the same error icons in data cells and the row indicator area.
See Also