<!-- Generated by tools/SharpCell.Docs from the XML documentation comments. Do not edit; run the tool with 'generate'. -->

# DataValidation

```csharp
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](SharpCell.DataValidation.md#list-string), [DataValidation.ListFrom](SharpCell.DataValidation.md#listfrom-string) or [DataValidation.Custom](SharpCell.DataValidation.md#custom-string). SharpCell keeps the rule with the cells and saves it; it does not check values.

## Constructors

<a id="ctor"></a>
### DataValidation()

```csharp
public DataValidation()
```

A rule allowing any value; set properties in an initializer.

<a id="ctor-datavalidation"></a>
### DataValidation(DataValidation)

```csharp
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

<a id="allowblank"></a>
### AllowBlank

```csharp
public bool AllowBlank { get; init; }
```

Whether an empty cell is accepted; true unless set.

<a id="errormessage"></a>
### ErrorMessage

```csharp
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.

<a id="errorstyle"></a>
### ErrorStyle

```csharp
public ValidationErrorStyle ErrorStyle { get; init; }
```

What Excel does with a value that breaks the rule; [ValidationErrorStyle.Stop](SharpCell.ValidationErrorStyle.md#stop) unless set.

<a id="errortitle"></a>
### ErrorTitle

```csharp
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.

<a id="formula1"></a>
### Formula1

```csharp
public string? Formula1 { get; init; }
```

The first formula, with a leading `=` (added if missing) as [Cell.Formula](SharpCell.Cell.md#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.

<a id="formula2"></a>
### Formula2

```csharp
public string? Formula2 { get; init; }
```

The second formula, the high end for [ValidationOperator.Between](SharpCell.ValidationOperator.md#between) and [ValidationOperator.NotBetween](SharpCell.ValidationOperator.md#notbetween), written like [DataValidation.Formula1](SharpCell.DataValidation.md#formula1); null for none.

<a id="inputmessage"></a>
### InputMessage

```csharp
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.

<a id="inputtitle"></a>
### InputTitle

```csharp
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.

<a id="operator"></a>
### Operator

```csharp
public ValidationOperator Operator { get; init; }
```

How a value is compared with [DataValidation.Formula1](SharpCell.DataValidation.md#formula1) and [DataValidation.Formula2](SharpCell.DataValidation.md#formula2); [ValidationOperator.Between](SharpCell.ValidationOperator.md#between) unless set. Lists and custom rules ignore it.

<a id="showdropdown"></a>
### ShowDropDown

```csharp
public bool ShowDropDown { get; init; }
```

Whether Excel shows the drop-down of a list rule; true unless set.

<a id="showerrormessage"></a>
### ShowErrorMessage

```csharp
public bool ShowErrorMessage { get; init; }
```

Whether Excel shows the error message when a value breaks the rule; true unless set.

<a id="showinputmessage"></a>
### ShowInputMessage

```csharp
public bool ShowInputMessage { get; init; }
```

Whether Excel shows the input message when a cell of the rule is selected; true unless set.

<a id="type"></a>
### Type

```csharp
public ValidationType Type { get; init; }
```

What the cells may hold; [ValidationType.Any](SharpCell.ValidationType.md#any) unless set.

## Methods

<a id="custom-string"></a>
### Custom(string)

```csharp
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.

<a id="equals-datavalidation"></a>
### Equals(DataValidation)

```csharp
public bool Equals(DataValidation? other)
```

Whether the other rule has the same settings; texts are compared ordinally.

<a id="equals-object"></a>
### Equals(object)

```csharp
public override bool Equals(object? obj)
```

Whether `obj` is a rule with the same settings.

<a id="gethashcode"></a>
### GetHashCode()

```csharp
public override int GetHashCode()
```

A hash code consistent with [DataValidation.Equals](SharpCell.DataValidation.md#equals-datavalidation).

<a id="list-string"></a>
### List(string[])

```csharp
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.

<a id="listfrom-string"></a>
### ListFrom(string)

```csharp
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.

<a id="tostring"></a>
### ToString()

```csharp
public override string ToString()
```

The type, the operator of a comparing type, and the formulas, such as `WholeNumber Between =1 =10`.
