Skip to main content
A newer version of this page is available. .

Cell Validation

  • 3 minutes to read

The GridControl allows you to validate new cell values. The image below shows a GridControl with an invalid cell value:

ValidationCells

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E1592.

Follow the steps below to validate a cell’s value:

Step 1. Handle a Validating Event

The GridControl provides the following events to validate cell values manually:

Note

The GridControl does not raise these events by default when changing cell values in code. Set the DataViewBase.AllowLeaveInvalidEditor property to true to enable raising events.

The code sample below shows how to validate the UnitPrice column’s cell values:

<dxg:GridControl Name="grid">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="ProductName" />
        <dxg:GridColumn FieldName="UnitPrice" Validate="OnValidate" />            
        <dxg:GridColumn FieldName="Discontinued" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView />
    </dxg:GridControl.View>
</dxg:GridControl>
void OnValidate(object sender, GridCellValidationEventArgs e) { }

Step 2. Validate a New Value

Use the following GridCellValidationEventArgs‘s properties to get cell values:

You can verify values according to validation rules after obtaining them. If a cell’s value fails validation, set the ValidationEventArgs.IsValid property to false. In this case, an end-user cannot move focus to another cell until the cell’s value is valid.

In the following code sample, the product’s price cannot be reduced by over 30% if the product is discontinued:

void OnValidate(object sender, GridCellValidationEventArgs e) {
    bool discontinued = ((Product)e.Row).Discontinued;
    if (discontinued) {
        double discount = 100 - (Convert.ToDouble(e.Value) * 100) / Convert.ToDouble(e.CellValue);
        if (!(discount > 0 && discount <= 30)) {
            e.IsValid = false;
        }
    }
}

Step 3. Indicate Errors

The GridControl displays an error icon within the cell if an edited cell’s value is invalid:

ValidationCellsError

Use the following GridCellValidationEventArgs‘s properties to customize the error indication:

The code sample below demonstrates how to show an error tooltip depending on the entered value:

void OnValidate(object sender, GridCellValidationEventArgs e) {
    bool discontinued = ((Product)e.Row).Discontinued;
    if (discontinued) {
        double discount = 100 - (Convert.ToDouble(e.Value) * 100) / Convert.ToDouble(e.CellValue);
        if (!(discount > 0 && discount <= 30)) {
            e.IsValid = false;
            e.ErrorType = ErrorType.Critical;
            if (discount < 0) {
                e.ErrorContent = string.Format("The price cannot be greater than ${0}", 
                    Convert.ToDouble(e.CellValue));
                return;
            }
            e.ErrorContent = string.Format("The discount cannot be greater than 30% (${0}). Please correct the price.", 
                Convert.ToDouble(e.CellValue)*0.7);
        }
    }
}