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 or finishes via IValidator.Validate.
  • When copying data into a validated cell or range (the target’s validator must accept the incoming text).

If validation fails, the keystroke or edit is cancelled and the cell keeps its previous value. 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?