DataValidation
public sealed class DataValidation
Namespace SharpCell · package SharpCell
A data validation rule: what the cells of a range accept and the messages Excel shows for them. Set properties in an initializer, or start from DataValidation.List, DataValidation.ListFrom or DataValidation.Custom. SharpCell keeps the rule with the cells and saves it; it does not check values.
Constructors
DataValidation()
public DataValidation()
A rule allowing any value; set properties in an initializer.
DataValidation(DataValidation)
public DataValidation(DataValidation source)
A copy of another rule, so a factory's rule can be changed in an initializer: new DataValidation(DataValidation.List("Yes", "No")) { ErrorMessage = "Choose Yes or No." }.
Parameters
source: The rule to copy.
Exceptions
ArgumentNullException: The source is null.
Properties
AllowBlank
public bool AllowBlank { get; init; }
Whether an empty cell is accepted; true unless set.
ErrorMessage
public string? ErrorMessage { get; init; }
The error message, at most 255 characters, or null for Excel's own.
Exceptions
ArgumentException: The message is longer than 255 characters.
ErrorStyle
public ValidationErrorStyle ErrorStyle { get; init; }
What Excel does with a value that breaks the rule; ValidationErrorStyle.Stop unless set.
ErrorTitle
public string? ErrorTitle { get; init; }
The title of the error message, at most 32 characters, or null for Excel's own.
Exceptions
ArgumentException: The title is longer than 32 characters.
Formula1
public string? Formula1 { get; init; }
The first formula, with a leading = (added if missing) as Cell.Formula takes it, or null: the low end or the value compared with, the list (="Yes,No" or =$H$1:$H$5) or the custom condition (=A1>0). Relative references are relative to the first cell of the rule's first range. It is parsed when the rule is added to a sheet.
Formula2
public string? Formula2 { get; init; }
The second formula, the high end for ValidationOperator.Between and ValidationOperator.NotBetween, written like DataValidation.Formula1; null for none.
InputMessage
public string? InputMessage { get; init; }
The input message, at most 255 characters, or null for none.
Exceptions
ArgumentException: The message is longer than 255 characters.
InputTitle
public string? InputTitle { get; init; }
The title of the input message, at most 32 characters, or null for none.
Exceptions
ArgumentException: The title is longer than 32 characters.
Operator
public ValidationOperator Operator { get; init; }
How a value is compared with DataValidation.Formula1 and DataValidation.Formula2; ValidationOperator.Between unless set. Lists and custom rules ignore it.
ShowDropDown
public bool ShowDropDown { get; init; }
Whether Excel shows the drop-down of a list rule; true unless set.
ShowErrorMessage
public bool ShowErrorMessage { get; init; }
Whether Excel shows the error message when a value breaks the rule; true unless set.
ShowInputMessage
public bool ShowInputMessage { get; init; }
Whether Excel shows the input message when a cell of the rule is selected; true unless set.
Type
public ValidationType Type { get; init; }
What the cells may hold; ValidationType.Any unless set.
Methods
Custom(string)
public static DataValidation Custom(string formula)
A rule accepting a value when the formula is true, such as Custom("=A1>0").
Parameters
formula: The condition, relative to the first cell of the rule's first range.
Exceptions
ArgumentException: The formula is null or empty.
Equals(DataValidation)
public bool Equals(DataValidation? other)
Whether the other rule has the same settings; texts are compared ordinally.
Equals(object)
public override bool Equals(object? obj)
Whether obj is a rule with the same settings.
GetHashCode()
public override int GetHashCode()
A hash code consistent with DataValidation.Equals.
List(string[])
public static DataValidation List(params string[] items)
A drop-down list of the given items, kept in the rule as one quoted literal: List("Yes", "No") has the formula ="Yes,No".
Parameters
items: The items, in order. An item cannot hold a comma, which separates them.
Exceptions
ArgumentNullException: The items or one of them is null.ArgumentException: There are no items, an item holds a comma, or the items with their commas are longer than 255 characters, Excel's limit.
ListFrom(string)
public static DataValidation ListFrom(string range)
A drop-down list of the values of a range, such as ListFrom("$H$1:$H$5") or a defined name.
Parameters
range: The range or name, with or without a leading=.
Exceptions
ArgumentException: The range is null or empty.
ToString()
public override string ToString()
The type, the operator of a comparing type, and the formulas, such as WholeNumber Between =1 =10.