ReoGrid
DOCUMENT
ReoGrid Action Framework

ReoGrid Action Framework

ReoGrid uses a lightweight action framework (based on unvell.Common.ActionManager) to record all operations, support undo/redo, and allow applications to group or customize edits. This guide shows how to use built‑in actions, compose them, create your own, and monitor what runs.

A quick example of ReoGrid actions.

// Select a worksheet
grid.CurrentWorksheet = mysheet;

// Create an action (set cell 'A1' to 100)
var action = new SetCellDataAction("A1", 100);

// Perform the action
grid.DoAction(action);

Undo and redo the last action

After undoing or redoing an action, the CurrentWorksheet will automatically switch to the target worksheet of the performed action.

grid.Undo();  // Undo
grid.Redo();  // Redo

Use built‑in actions

Update a cell value

using unvell.ReoGrid;
using unvell.ReoGrid.Actions;

var sheet = grid.CurrentWorksheet;
var action = new SetCellDataAction(2, 1, "Hello"); // row 2, column 1 (zero-based)
grid.DoAction(sheet, action);

Delete rows

using unvell.ReoGrid;
using unvell.ReoGrid.Actions;

var sheet = grid.CurrentWorksheet;
var deleteRows = new RemoveRowsAction(row: 5, rows: 3);
grid.DoAction(sheet, deleteRows);

After either call, ReoGrid pushes one undo item. Undo/Redo on the control will reverse/redo the action automatically.

Understand the action types

  • WorksheetAction
    Base class for most sheet-scoped operations. Provides Do/Undo plus a Worksheet reference that the control sets before execution.
  • WorksheetReusableAction
    Extends WorksheetAction so the same operation can be cloned and repeated on another range. ReoGrid uses this to support the “Repeat” command.
  • WorksheetActionGroup
    Wraps several WorksheetAction instances so they are executed and undone as a single history item. Use this to merge related edits (e.g., updating dependent columns):
    var group = new WorksheetActionGroup();
    group.Actions.Add(new SetCellDataAction(row, 0, key));
    group.Actions.Add(new SetCellDataAction(row, 1, LookupPrice(key)));
    group.Actions.Add(new SetCellDataAction(row, 2, LookupDescription(key)));
    
    grid.DoAction(sheet, group); // one undo item
    
  • WorksheetReusableActionGroup
    A grouped version that is also repeatable. Provide a target range when constructing:
    var repeatable = new WorksheetReusableActionGroup(new RangePosition("B2:D5"));
    repeatable.Actions.Add(new SetRangeStyleAction(repeatable.Range, new WorksheetRangeStyle
    {
        Flag = PlainStyleFlag.BackColor,
        BackColor = Color.LightYellow,
    }));
    
    grid.DoAction(sheet, repeatable);
    // Later you can clone onto another range:
    grid.RepeatLastAction(sheet, new RangePosition("F2:H5"));
    

Create a custom action

Implement WorksheetAction (or WorksheetReusableAction if you need repeat) and override Do, Undo, and GetName.

using unvell.ReoGrid.Actions;

class ToggleRowHighlightAction : WorksheetAction
{
    private readonly int row;
    private bool[]? originalStates;

    public ToggleRowHighlightAction(int row) { this.row = row; }

    public override void Do()
    {
        originalStates = new bool[Worksheet.ColumnCount];
        for (int c = 0; c < Worksheet.ColumnCount; c++)
        {
            var cell = Worksheet.CreateAndGetCell(row, c);
            originalStates[c] = cell.Style.Bold;
            cell.Style.Bold = !cell.Style.Bold;
        }
    }

    public override void Undo()
    {
        if (originalStates == null) return;
        for (int c = 0; c < Worksheet.ColumnCount && c < originalStates.Length; c++)
        {
            var cell = Worksheet.CreateAndGetCell(row, c);
            cell.Style.Bold = originalStates[c];
        }
    }

    public override string GetName() => "Toggle Row Highlight";
}

Run it exactly like a built‑in action: grid.DoAction(sheet, new ToggleRowHighlightAction(3));.

Monitor action execution

ReoGrid exposes events you can subscribe to in order to inspect, log, or replace actions.

Using control-level events

grid.BeforeActionPerform += (s, e) =>
{
    // e.Action is about to run; set e.IsCancelled = true to stop it.
};

grid.ActionPerformed += (s, e) =>
{
    // e.Action finished (Do/Redo). Use grid.Undid and grid.Redid for undo/redo hooks.
};

grid.Undid += (s, e) => { /* last action was undone */ };
grid.Redid += (s, e) => { /* last action was redone */ };

Inspect ActionBehavior (Do/Undo/Redo)

The underlying ActionManager raises BeforePerformAction and AfterPerformAction with ActionEventArgs.Behavior set to ActionBehavior.Do, Undo, or Redo. When you need that detail (e.g., logging), hook those events. Example inside a custom control or when you have access to the ActionManager instance:

actionManager.AfterPerformAction += (s, e) =>
{
    switch (e.Behavior)
    {
        case ActionBehavior.Do:
            Log($"Do: {e.Action.GetName()}");
            break;
        case ActionBehavior.Undo:
            Log($"Undo: {e.Action.GetName()}");
            break;
        case ActionBehavior.Redo:
            Log($"Redo: {e.Action.GetName()}");
            break;
    }
};

Cancel an action and replace it

You can intercept a user action, cancel it, and perform your own (possibly grouped) action instead:

grid.BeforeActionPerform += (s, e) =>
{
    if (e.Action is SetCellDataAction set && set.Column == 0)
    {
        e.IsCancelled = true; // skip default

        var group = new WorksheetActionGroup();
        group.Actions.Add(set); // keep the original edit
        group.Actions.Add(new SetCellDataAction(set.Row, 1, "auto-fill"));
        grid.DoAction(grid.CurrentWorksheet, group);
    }
};

This pattern is useful when dependent updates should be recorded as one undoable operation.

Actions Across Multiple Worksheets

Actions can be performed on different worksheets within the same workbook. ReoGrid preserves the execution order of actions, even when they target different worksheets. When you undo or redo actions, they are processed in the same order as they were originally performed, regardless of the worksheet.

For example, if you update cells on Sheet1 and then on Sheet2, undoing will revert the last action first (on Sheet2), followed by the previous action (on Sheet1). Redo will apply them in the original sequence.

158 When undoing, actions are reverted in reverse order. 159

This ensures consistent history management across all worksheets in your workbook.


Was the content of the page helpful?

© 2012-2025UNVELL Inc.