Skip to main content

Data Validation

  • 4 minutes to read

This document introduces the Data Validation concept and demonstrates how to manage data validation rules in a worksheet using the Data Validation API.


Data Validation Overview

Data validation allows you to restrict data that can be entered into a cell. It assists the users in entering data that is valid. You can allow end-users to enter invalid data, but then warn them when that occurs. You can also provide messages to explain the reason for the restriction or to instruct users how to correct errors.

End-users are provided with the Data Tools command group located on the Data Ribbon tab to invoke a Data Validation dialog and to identify cells with invalid data by displaying a red circle around them.


When a user attempts to enter a value that is not correct according to validation rules, an alert is shown. Alert text is specified with the ASPxSpreadsheetStringId.DataValidation enumeration member so it can be easily localized, as described in the Localization topic.

You can also provide a custom message that explains what data can be entered in a cell, and a custom error dialog that appears when incorrect data is entered.

Data Validation API

The DataValidation interface provides information describing the data validation rule - the DataValidation.Range in which the rule is in effect, the DataValidation.ValidationType and other options which can be used while validating data.

Data validation rules in a worksheet are contained in the DataValidationCollection collection, which is accessible using the Worksheet.DataValidations property.

To add a new data validation rule, use the DataValidationCollection.Add method of the collection. To cancel a rule, remove it from the collection. To remove all data validation rules, clear the collection.

Data Validation Common Tasks


API Members

User Interface

Restrict data to predefined items on a list.



Restrict numbers outside a specified range.





Restrict dates and times outside a certain time frame.




Limit the number of text characters.




Validate data based on formulas or values in other cells.



Create a message that explains what data can be entered in a cell.





Create a message that appears when the end-user types incorrect data.






The code snippet below illustrates how to create and apply different validation criteria to worksheet cells using Data Validation API.

View Example

Worksheet worksheet = workbook.Worksheets[0];
worksheet["C1"].NumberFormat = "mmm/d/yyyy h:mm";

// Restrict data entry to a whole number from 10 to 20.
worksheet.DataValidations.Add(worksheet["B1"], DataValidationType.WholeNumber, DataValidationOperator.Between, 10, 20);

// Restrict data entry to a number within limits.
DataValidation validation = worksheet.DataValidations.Add(worksheet["F4:F11"], DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);

// Restrict data entry using criteria calculated by a worksheet formula.
worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");

// Restrict data entry to 3 symbols.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);

// Restrict data entry to values in a drop-down list specified in code. 
// Note that the list in code should always use comma to separate entries, 
// but the list in UI is displayed using culture-specific list separator.
worksheet.DataValidations.Add(worksheet["A4:A11"], DataValidationType.List, "PASS, FAIL");

// Restrict data entry to values in a drop-down list obtained from a worksheet.
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));

// Restrict data entry to a time before the specified time.
worksheet.DataValidations.Add(worksheet["C1"], DataValidationType.Time, DataValidationOperator.LessThanOrEqual, DateTime.Now);

// Highlight data validation ranges.
worksheet["H4:H9"].FillColor = Color.LightGray;
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3, 0xFFDFC4, 0xFFDAE9};
for (int i = 0; i < worksheet.DataValidations.Count; i++){
    worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
See Also