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

Data Validation

  • 5 minutes to read

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

Data Validation Overview

Data validation ensures that a cell only accepts valid data. When users enter invalid data, a message can be displayed that explains why the value is invalid and/or how to correct the error.

The SpreadsheetControl provides 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.

DataValidationRibbon

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 XtraSpreadsheetStringId.Msg_DataValidationFailed 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 via the Worksheet.DataValidations property.

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

Use the DataValidationCollection.GetDataValidation method to retrieve data validation rules for the specified cell. The DataValidationCollection.GetDataValidations method obtains data validation entries for cells in the specified range or that meet certain criteria.

Data Validation Common Tasks

Task

API Members

User Interface

Restrict data to predefined items in a list.

DataValidationType.List

Dv_DropDownList_Worksheet

Restrict numbers outside a specified range.

DataValidationType.WholeNumber

DataValidationType.Decimal

DataValidationOperator.Between

DV_WholeNumberFrom10To20

Restrict dates and times outside a certain time frame.

DataValidationType.Time

DataValidationOperator.LessThanOrEqual

DV_DateTimeNow

Limit the number of text characters.

DataValidationType.TextLength

DataValidationOperator.Equal

DV_TextLength

Validate data based on formulas or values in other cells.

DataValidationType.Custom

DV_CalculatedCriteria

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

DataValidation.InputTitle

DataValidation.InputMessage

DataValidation.ShowInputMessage

DV_ShowInputMessage_Dialog

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

DataValidation.ErrorTitle

DataValidation.ErrorMessage

DataValidation.ErrorStyle

DataValidation.ShowErrorMessage

DV_ShowErrorMessage_Dialog

Display or hide circles around invalid data.

The SpreadsheetControl circles only first 255 invalid cells,

other invalid entries are ignored.

SpreadsheetControl.CircleInvalidData

SpreadsheetControl.ClearValidationCircles

Spreadsheet_DataValidation_CircleInvalidData

Retrieve cells that do not meet the data validation criteria.

DataValidationCollection.GetInvalidCells()

Check whether the cell contains valid data.

DataValidationCollection.Validate

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

Note

The maximum length of a list used to specify the DataValidationType.List validation type or a custom validation criteria is 255 characters. When a list or criteria exceeds this value, an InvalidOperationException is thrown.

View Example

workbook.LoadDocument("Documents\\DataValidation.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
worksheet["C1"].SetValue(DateTime.Now);
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]);
}