Back to Devexpress

DataValidationCollection.Add(CellRange, DataValidationType, ValueObject) Method

officefileapi-devexpress-dot-spreadsheet-dot-datavalidationcollection-dot-add-x28-devexpress-dot-spreadsheet-dot-cellrange-devexpress-dot-spreadsheet-dot-datavalidationtype-devexpress-dot-spreadsheet-dot-valueobject-x29.md

latest6.1 KB
Original Source

DataValidationCollection.Add(CellRange, DataValidationType, ValueObject) Method

Creates a new data validation entry and adds it to the document’s collection.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
DataValidation Add(
    CellRange range,
    DataValidationType validationType,
    ValueObject criteria
)
vb
Function Add(
    range As CellRange,
    validationType As DataValidationType,
    criteria As ValueObject
) As DataValidation

Parameters

NameTypeDescription
rangeCellRange

A CellRange object that is the worksheet range to which the data validation rule is applied.

| | validationType | DataValidationType |

A DataValidationType enumeration member that specifies the validation type.

| | criteria | ValueObject |

A ValueObject that is the value used in the criterion.

|

Returns

TypeDescription
DataValidation

A DataValidation object that specifies a validation rule for a worksheet range.

|

Remarks

This method overload is used for validation types DataValidationType.Custom and DataValidationType.List.

For the DataValidationType.Custom type, the criteria is a string which can be recognized as a worksheet formula. Therefore, it must begin with an equal sign and evaluate to either true or false.

Note

An individual worksheet cell can have only one data validation rule associated with it. If subsequent Add method calls affect the same cell, only the data validation rule created in the last call is applied.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(CellRange, DataValidationType, ValueObject) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheetcontrol-api-part-3/CS/SpreadsheetControl_API_Part03/CodeExamples/DataValidationActions.cs#L24

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

wpf-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/DataValidationActions.cs#L36

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

spreadsheet-document-api-examples-part2/CS/SpreadsheetDocServerAPIPart2/CodeExamples/DataValidationActions.cs#L42

csharp
// Restrict data entry based on a formula.
worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");

winforms-spreadsheetcontrol-api-part-3/VB/SpreadsheetControl_API_Part03/CodeExamples/DataValidationActions.vb#L21

vb
' Restrict data entry using criteria calculated by a worksheet formula.
worksheet.DataValidations.Add(worksheet("B4:B11"), DevExpress.Spreadsheet.DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)")
' Restrict data entry to 3 symbols.

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/DataValidationActions.vb#L40

vb
' Restrict data entry based on a formula.
worksheet.DataValidations.Add(worksheet("B4:B11"), DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)")

See Also

DataValidationCollection Interface

DataValidationCollection Members

DevExpress.Spreadsheet Namespace