Class DataValidation
Represents a data validation rule. A data validation rule can be applied to a range of cells. The rule specifies the type of data that can be entered in the cells of the range. Additionally, the rule can limit the values to values in a list of allowable values that is specified by the user.
Inheritance
Namespace: IronXL.DataValidations
Assembly: IronXL.dll
Syntax
public class DataValidation : Object
DataValidation is the rule a developer reads back to see how a worksheet restricts what a user may type into a cell. It describes one validation constraint: the kind of data allowed, the comparison and bounds it must satisfy, and the prompt and error messages Excel shows. It is what a developer inspects behind a search like "C# Excel data validation rule".
A DataValidation is created indirectly. The worksheet's DataValidations collection exposes typed factory methods such as AddIntegerRule, AddDecimalRule, AddDateRule, AddTextLengthRule, AddStringListRule, and AddFormulaListRule that build the rule and attach it to a range. The properties on the resulting DataValidation then describe and refine that rule, and Remove takes the same object back out of the collection.
The properties fall into three groups. The constraint group sets what is valid: ConstraintType names the data kind (whole number, decimal, date, list, and similar), ComparisonOperator from the IronXL.DataValidations.ComparisonOperator set chooses the test such as Between or GreaterThan, and Formula1 and Formula2 carry the bounds, with Formula2 used by range operators. ConstrainedRangeAddress reports the cells the rule covers and EmptyCellAllowed decides whether a blank passes. The prompt group, ShowPromptBox, PromptBoxTitle, and PromptBoxText, sets the hint shown when a cell is selected. The error group, ShowErrorBox, ErrorBoxTitle, ErrorBoxText, and ErrorAlert, sets the message and its severity when an entry is rejected, and ShowDropDownList toggles the in-cell picker for list rules. Note that this ComparisonOperator is the data validation type, separate from the same-named operator in IronXL.Formatting.Enums used for conditional formatting.
using IronXL;
using IronXL.DataValidations;
WorkSheet sheet = WorkBook.Create().DefaultWorkSheet;
DataValidation rule =
sheet.DataValidations.AddIntegerRule("A1:A10", ComparisonOperator.Between, 1, 100);
rule.ErrorBoxText = "Enter a value from 1 to 100.";The data validation example shows rule-driven cell handling, the select range how-to covers the ranges a rule targets, and the set cell data format how-to covers related cell controls.
Properties
ComparisonOperator
The operator used by the data validation rule. This property is only used for whole number, decimal, date, time, and text length data validation rules. For all other types of data validation rules, this property returns IronXL.DataValidations.ComparisonOperator.None.
Declaration
public ComparisonOperator ComparisonOperator { get; set; }
Property Value
| Type | Description |
|---|---|
| ComparisonOperator |
ConstrainedRangeAddress
The range of cells to which this data validation rule applies.
Declaration
public RangeAddress ConstrainedRangeAddress { get; }
Property Value
| Type | Description |
|---|---|
| RangeAddress |
ConstraintType
The type of data validation rule.
Declaration
public ConstraintType ConstraintType { get; }
Property Value
| Type | Description |
|---|---|
| ConstraintType |
EmptyCellAllowed
Controlls wether the empty cell is allowed by the data validation rule.
Declaration
public bool EmptyCellAllowed { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
ErrorAlert
The style of error alert used by the data validation rule.
- Stop Displays an error alert dialog box with a Stop icon in its title bar.
- Warning Displays an error alert dialog box with a Warning icon in its title bar.
- Information Displays an error alert dialog box with an Information icon in its title bar.
Declaration
public ErrorAlert ErrorAlert { get; set; }
Property Value
| Type | Description |
|---|---|
| ErrorAlert |
ErrorBoxText
The text of the error alert dialog box.
Declaration
public string ErrorBoxText { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
ErrorBoxTitle
The title of the error alert dialog box.
Declaration
public string ErrorBoxTitle { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Formula1
The first formula used by the data validation rule. For list data validation rules, this formula specifies the list of allowable values. For Custom data validation rules, this formula specifies the custom validation formula.
Declaration
public string Formula1 { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
Formula2
The second formula used by the data validation rule. This formula is only used by whole number, decimal, date, time, and text length data validation rules. For all other types of data validation rules, this property returns System.String.Empty or null.
Declaration
public string Formula2 { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
PromptBoxText
The title of the prompt box.
Declaration
public string PromptBoxText { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
PromptBoxTitle
The text of the prompt box.
Declaration
public string PromptBoxTitle { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
ShowDropDownList
Controlls wether the dropdown arrow is shown in the cell. This property is only used for list and custom data validation rules. It will return false for all other types of data validation rules.
Declaration
public bool ShowDropDownList { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
ShowErrorBox
Controlls wether the error alert is shown when the data validation rule is violated.
Declaration
public bool ShowErrorBox { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
ShowPromptBox
Controlls wether the prompt box is shown when the cell is selected.
Declaration
public bool ShowPromptBox { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Methods
Equals(Object)
An override for the System.Object.ToString method.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | obj | An object to compare with this instance. |
Returns
| Type | Description |
|---|---|
| System.Boolean | true if |
GetHashCode()
An override for the System.Object.GetHashCode method.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| System.Int32 | A hash code for this instance. |