ReoGrid provides cell-level and worksheet-level protection to prevent users from modifying data. This includes locking individual cells, locking the entire worksheet, and making the workbook read-only.
Cell-Level Locking
Each cell has an IsLocked property of type CellLock that controls whether the cell can be edited.
CellLock Enum
| Value | Description |
|---|---|
Inherit | Inherits the lock state from the worksheet (default) |
Locked | Cell is explicitly locked — cannot be edited |
Unlocked | Cell is explicitly unlocked — can always be edited |
Lock Individual Cells
// Lock a specific cell
sheet.Cells["A1"].IsLocked = CellLock.Locked;
// Unlock a specific cell
sheet.Cells["B2"].IsLocked = CellLock.Unlocked;
// Reset to inherit from worksheet
sheet.Cells["A1"].IsLocked = CellLock.Inherit;
Lock a Range of Cells
To lock multiple cells, iterate over them:
sheet.IterateCells("A1:D10", (row, col, cell) =>
{
if (cell != null) cell.IsLocked = CellLock.Locked;
return true; // continue iteration
});
Cell ReadOnly Property
Individual cells also have a IsReadOnly property for a simpler read-only flag:
sheet.Cells["A1"].IsReadOnly = true;
Note: When a locked cell is being edited, setting
IsLockedtoLockedimmediately ends the edit session. IfAlwaysMoveSelectionToUnlockedCellis enabled, the selection also moves to the nearest unlocked cell.
Worksheet-Level Locking
Lock or unlock the entire worksheet:
// Lock the entire worksheet
sheet.IsLocked = true;
// Unlock the entire worksheet
sheet.IsLocked = false;
When a worksheet is locked:
- Cells with
IsLocked = CellLock.Inheritbecome locked - Cells with
IsLocked = CellLock.Unlockedremain editable - Any active edit session is ended immediately
Selectable Locked Cells
By default, locked cells can still be selected. To prevent selection of locked cells:
sheet.IsLockedCellSelectable = false;
Move Selection to Unlocked Cell
Programmatically move the selection to the nearest unlocked cell:
sheet.MoveSelectionRangeToUnlockedCell();
Auto-Move to Unlocked Cell
Automatically move selection to unlocked cells when the worksheet is locked:
sheet.Options.AlwaysMoveSelectionToUnlockedCell = true;
Workbook-Level Read-Only
Set the entire workbook (all worksheets) to read-only mode:
// Make the entire workbook read-only
reoGridControl.Readonly = true;
// Make it writable again
reoGridControl.Readonly = false;
This sets the Edit_Readonly worksheet setting on all worksheets.
Worksheet Read-Only Setting
Use the worksheet settings to make individual worksheets read-only:
// Make a single worksheet read-only
sheet.SetSettings(WorksheetSettings.Edit_Readonly, true);
// Check if read-only
bool isReadonly = sheet.HasSettings(WorksheetSettings.Edit_Readonly);
// Make writable
sheet.SetSettings(WorksheetSettings.Edit_Readonly, false);
Range Read-Only
Make a range of cells read-only via the range instance:
var range = sheet.Ranges["A1:D10"];
range.IsReadonly = true;
Custom Cell Body and Read-Only
Custom cell bodies (like buttons, checkboxes, etc.) are automatically disabled when their owner cell is read-only. This behavior is controlled by the DisableWhenCellReadonly property:
public class MyCustomCell : CellBody
{
// Override to keep the cell body active even when the cell is read-only
public override bool DisableWhenCellReadonly => false;
}
Exception Handling
When code attempts to modify a read-only cell, ReoGrid throws specific exceptions:
| Exception | Description |
|---|---|
OperationOnReadonlyCellException | Base exception for read-only violations |
CellDataReadonlyException | Attempt to modify data of a read-only cell (provides CellPos) |
RangeContainsReadonlyCellsException | Operation on a range containing read-only cells (provides Range) |
Handle these exceptions:
try
{
sheet["A1"] = "new value";
}
catch (CellDataReadonlyException ex)
{
Console.WriteLine($"Cannot modify cell at {ex.CellPos}: cell is read-only");
}
catch (RangeContainsReadonlyCellsException ex)
{
Console.WriteLine($"Range {ex.Range} contains read-only cells");
}
Common Patterns
Template with Editable Areas
Create a form template where only specific cells are editable:
var sheet = grid.CurrentWorksheet;
// Set up the template with labels
sheet["A1"] = "Name:";
sheet["A2"] = "Email:";
sheet["A3"] = "Phone:";
// Make the label cells locked
sheet.Cells["A1"].IsLocked = CellLock.Locked;
sheet.Cells["A2"].IsLocked = CellLock.Locked;
sheet.Cells["A3"].IsLocked = CellLock.Locked;
// Explicitly unlock the input cells
sheet.Cells["B1"].IsLocked = CellLock.Unlocked;
sheet.Cells["B2"].IsLocked = CellLock.Unlocked;
sheet.Cells["B3"].IsLocked = CellLock.Unlocked;
// Lock the worksheet — only B1:B3 remain editable
sheet.IsLocked = true;
// Optionally auto-move to unlocked cells
sheet.Options.AlwaysMoveSelectionToUnlockedCell = true;
Related Topics
- Cell Editing — Cell edit behavior
- Worksheet Settings — All worksheet settings
- Data Validation — Validate user input