Skip to main content
Row

DataValidationCollection.GetDataValidation(Cell) Method

Obtains a data validation entry for the specified cell.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v24.1.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

DataValidation GetDataValidation(
    Cell cell
)

Parameters

Name Type Description
cell Cell

A Cell object specifying a worksheet cell.

Returns

Type Description
DataValidation

A DataValidation object which contains data validation rules for the specified cell or null if data validation for this cell is not defined.

Example

View Example

workbook.LoadDocument("Documents\\DataValidation.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

// Add data validations.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));

// Get data validation entry associated with a particular cell.
worksheet.DataValidations.GetDataValidation(worksheet.Cells["E4"]).Criteria = ValueObject.FromRange(worksheet["H4:H5"]);

// Get data validation entries for the specified range.
var myValidation = worksheet.DataValidations.GetDataValidations(worksheet["D4:E11"])
    .Where(d => d.ValidationType == DataValidationType.TextLength).SingleOrDefault();
if (myValidation != null) myValidation.Criteria = 4;

// Get data validation entries that meet certain criteria.
foreach (var d in worksheet.DataValidations.GetDataValidations(DataValidationType.TextLength, DataValidationOperator.Equal, 4, ValueObject.Empty))
{
    // Change criteria operator.
    // Range D4:D11 should contain text with more than 4 characters.
    d.Operator = DataValidationOperator.GreaterThan;
}              

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