V4 V5

Data validation

ReoGrid can block invalid input while a user edits cells. Attach an IValidator to a cell or range to reject characters and final text that fail your rules.

Where it runs

  • During edit typing (per character) via IValidator.IsValidChar.
  • When the edit text changes via IValidator.Validate, including for each typed character.
  • When copying data into a validated cell or range (the target’s validator must accept the incoming text).

If validation of a typed character fails, the keystroke is cancelled. If validation of the whole text fails (for example, on paste), the text stays in the editor and is applied when the edit is committed. ReoGrid does not show a built-in message; expose one yourself if needed (for example, via your validator or UI).

Apply a validator

using unvell.ReoGrid.Edit;

var sheet = grid.CurrentWorksheet;

// Apply to a range (all cells share the validator instance)
sheet.Ranges["C1:C20"].Validator = new NumericValidator(); // digits, '-' or '.'

// Clear validation
sheet.Ranges["C1:C20"].Validator = null;

Built-in numeric validator

NumericValidator accepts digits, a minus sign, and a decimal point by default. Pass a regex pattern to change what counts as numeric:

// allow digits only
sheet.Ranges["D1:D50"].Validator = new NumericValidator("^[0-9]+$");

Create a custom validator

Implement IValidator for your own rules (length, whitelist, regex, etc.).

using System.Collections.Generic;
using System.Text.RegularExpressions;
using unvell.ReoGrid.Edit;

class UppercaseValidator : IValidator
{
    private readonly Regex pattern = new Regex("^[A-Z]*$");

    public bool IsValidChar(string text) => pattern.IsMatch(text);

    public IList<IValidationFailure> Validate(string text)
    {
        if (pattern.IsMatch(text)) return null;
        return new List<IValidationFailure> { new SimpleFailure("Uppercase letters only") };
    }
}

class SimpleFailure : IValidationFailure
{
    public string Message { get; }
    public SimpleFailure(string message) { Message = message; }
}

// Apply
sheet.Ranges["A1:A100"].Validator = new UppercaseValidator();

Tips

  • Keep validators fast — methods run on every key press.
  • If you want to show messages, handle failed validation in your UI (for example, subscribe to edit events and inspect your validator).
  • Validation is enforced for user edits and copy/paste into validated cells; values assigned programmatically in code should already be correct.

Listen for validation status

ReoGrid raises Worksheet.CellValidation every time a validator runs. Use it to surface “OK/NG” status or custom messages:

sheet.CellValidation += (s, e) =>
{
    if (!e.IsValid)
    {
        var msg = string.Join("\n", e.Failures?.Select(f => f.Message) ?? Array.Empty<string>());
        ShowStatus($"Validation failed at {e.Cell?.Position}: {msg}");
        // e.IsValid = true; // optionally override and allow
    }
    else
    {
        ShowStatus("OK");
    }
};

You can also perform quick, inline checks in the edit events (CellEditTextChanging / CellEditCharInputed) for simple scenarios — see Cell Edit.

Was this article helpful?