Back to Devexpress

DataValidationCollection.Validate(Cell, CellValue) Method

officefileapi-devexpress-dot-spreadsheet-dot-datavalidationcollection-dot-validate-x28-devexpress-dot-spreadsheet-dot-cell-devexpress-dot-spreadsheet-dot-cellvalue-x29.md

latest6.6 KB
Original Source

DataValidationCollection.Validate(Cell, CellValue) Method

Validates the specified value before assigning it to the specified cell.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
bool Validate(
    Cell cell,
    CellValue newValue
)
vb
Function Validate(
    cell As Cell,
    newValue As CellValue
) As Boolean

Parameters

NameTypeDescription
cellCell

A Cell object that is a cell with the data validation applied.

| | newValue | CellValue |

A CellValue object that is a value to be validated.

|

Returns

TypeDescription
Boolean

true , if the specified value meets the data validation criteria applied to the specified cell; otherwise, false.

|

Remarks

Use the Validate method to check whether a value that should be assigned to the specified cell meets the data validation criteria applied to that cell.

If the newValue is empty, the Validate method’s result depends on the DataValidation.AllowBlank property of the data validation rule applied to the specified cell. If a cell does not have any data validation applied, the Validate method always returns true.

Use the other Validate method overload to check whether a particular cell in a worksheet contains valid data.

The DataValidationCollection.GetInvalidCells method allows you to obtain the collection of cells that do not match the data validation criteria in a worksheet.

The code sample below shows how to use the Validate method to check whether the cell value meets the validation criteria:

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

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

//Check whether the cell value meets the validation criteria:
bool isValid = worksheet.DataValidations.Validate(worksheet.Cells["D4"], worksheet.Cells["J4"].Value);
if (isValid)
{
    worksheet["D4"].CopyFrom(worksheet["J4"]);
}
vb
workbook.LoadDocument("Documents\DataValidation.xlsx")
Dim worksheet As Worksheet = workbook.Worksheets(0)

' Add data validations.
worksheet.DataValidations.Add(worksheet("D4:D11"), DataValidationType.TextLength, DataValidationOperator.Equal, 3)

'Check whether the cell value meets the validation criteria:
Dim isValid As Boolean = worksheet.DataValidations.Validate(worksheet.Cells("D4"), worksheet.Cells("J4").Value)
If isValid Then
  worksheet("D4").CopyFrom(worksheet("J4"))
End If

The following code snippets (auto-collected from DevExpress Examples) contain references to the Validate(Cell, CellValue) 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#L179

csharp
//Check whether the cell value meets the validation criteria:
bool isValid = worksheet.DataValidations.Validate(worksheet.Cells["D4"], worksheet.Cells["J4"].Value);
if (isValid) { worksheet["D4"].CopyFrom(worksheet["J4"]); }

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

csharp
// Check whether the cell value meets a validation criteria.
bool isValid = worksheet.DataValidations.Validate(worksheet.Cells["D4"], worksheet.Cells["J4"].Value);
if (isValid) { worksheet["D4"].CopyFrom(worksheet["J4"]); }

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

vb
'Check whether the cell value meets the validation criteria:
Dim isValid As Boolean = worksheet.DataValidations.Validate(worksheet.Cells("D4"), worksheet.Cells(CStr(("J4"))).Value)
If isValid Then

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

vb
' Check whether the cell value meets a validation criteria.
Dim isValid As Boolean = worksheet.DataValidations.Validate(worksheet.Cells("D4"), worksheet.Cells("J4").Value)
If isValid Then

See Also

GetInvalidCells()

DataValidationCollection Interface

DataValidationCollection Members

DevExpress.Spreadsheet Namespace