Ranges in ReoGrid are defined by a starting position and size. A range encompasses one or more cells. Ranges can intersect, and a range is not the same as a merged cell (though a range can be merged).

Ranges

ReoGrid represents ranges through two primary types:

  • RangePosition โ€” A lightweight struct holding position and size. Not tied to any worksheet.
  • ReferenceRange โ€” A class bound to a specific worksheet, providing direct access to data, styles, and operations.

RangePosition

The RangePosition struct stores numeric information for identifying a range: start row, start column, row count, and column count. All indices are zero-based.

// From an address string
var range = new RangePosition("C5:H14");

// From row, column, rows, columns
var range = new RangePosition(4, 2, 10, 6);

// From start and end positions
var range = new RangePosition(new CellPosition("C5"), new CellPosition("H14"));

// From two address strings
var range = new RangePosition("C5", "H14");

// Single cell
var range = new RangePosition(new CellPosition("A1"));

Range

Properties

PropertyTypeDescription
RowintStart row index
ColintStart column index
RowsintNumber of rows
ColsintNumber of columns
EndRowintEnd row index
EndColintEnd column index
StartPosCellPositionStart position
EndPosCellPositionEnd position
IsEmptyboolWhether rows or columns is zero
IsEntireboolWhether the range covers entire rows and columns
IsSingleCellboolWhether the range contains exactly one cell

Methods

MethodReturnDescription
Contains(CellPosition pos)boolCheck if a cell position is within the range
Contains(int row, int col)boolCheck if a row/column is within the range
Contains(RangePosition range)boolCheck if another range is entirely contained
ContainsRow(int row)boolCheck if a row is within the range
ContainsColumn(int col)boolCheck if a column is within the range
IntersectWith(RangePosition range)boolCheck if two ranges overlap
Offset(int rows, int cols)RangePositionCreate a new range offset by rows and columns
Offset(CellPosition pos)RangePositionCreate a new range offset by a cell position
ToAddress()stringConvert to address string (e.g., "A1:B10")
ToAbsoluteAddress()stringConvert to absolute address (e.g., "$A$1:$B$10")
ToRelativeAddress()stringConvert to relative address
SetRows(int rows)voidSet the number of rows
SetCols(int cols)voidSet the number of columns

Static Members

MemberDescription
RangePosition.EmptyAn empty range (0, 0, 0, 0)
RangePosition.EntireRangeThe entire worksheet range
RangePosition.IsValidAddress(string)Check if a string is a valid range address
RangePosition.Union(range1, range2)Get the minimum range containing both ranges
RangePosition.FromCellPosition(startRow, startCol, endRow, endCol)Create from cell coordinates

Get a Safe Range

When a range might exceed worksheet bounds, use FixRange:

var safeRange = sheet.FixRange(range);

ReferenceRange

A ReferenceRange is bound to a specific worksheet and provides direct access to data, styles, borders, formatting, and operations on that range.

Get a ReferenceRange

Use the Ranges collection on the worksheet:

var range = sheet.Ranges["B2:D3"];
var range = sheet.Ranges[1, 1, 2, 3];         // row, col, rows, cols
var range = sheet.Ranges[new RangePosition(1, 1, 2, 3)];
var range = sheet.Ranges[new CellPosition("B2"), new CellPosition("D3")];

Properties

Position

PropertyTypeDescription
PositionRangePositionRange position on the worksheet
WorksheetWorksheetThe parent worksheet
RowintStart row index
ColumnintStart column index
RowsintNumber of rows
ColsintNumber of columns
EndRowintEnd row index
EndColumnintEnd column index
StartPosCellPositionStart position
EndPosCellPositionEnd position

Data and Content

PropertyTypeDescription
DataobjectGet or set data for the entire range
CellsCellCollectionCollection of cell instances in the range
// Set range data
range.Data = new object[] { "Product", "Price", "Quantity" };

Set Range Data

Styling

PropertyTypeDescription
StyleReferenceRangeStyleStyle access for the range
BorderRangeBorderPropertyBorder properties wrapper
range.Style.BackColor = Color.LightBlue;
range.Style.Bold = true;

Border Shortcuts

PropertyTypeDescription
BorderOutsideRangeBorderStyleOutside borders
BorderAllRangeBorderStyleAll borders (outside + inside)
BorderLeftRangeBorderStyleLeft border
BorderTopRangeBorderStyleTop border
BorderRightRangeBorderStyleRight border
BorderBottomRangeBorderStyleBottom border
BorderInsideAllRangeBorderStyleAll inside borders
BorderInsideHorizontalRangeBorderStyleInside horizontal borders
BorderInsideVerticalRangeBorderStyleInside vertical borders
range.BorderOutside = RangeBorderStyle.BlackSolid;
range.Border.Outside = RangeBorderStyle.BlackSolid;  // equivalent

Data Format

PropertyTypeDescription
DataFormatCellDataFormatFlagData format type for all cells in the range
DataFormatArgsobjectFormat arguments
CustomDataFormatterIDataFormatterCustom formatter

Protection

PropertyTypeDescription
IsReadonlyboolSet all cells in the range as read-only
IsLockedCellLockLock state for all cells in the range (set only)
IsMergedCellboolWhether the range is a single merged cell (read-only)

Validation

PropertyTypeDescription
ValidatorIValidatorInput validation rule for the range

Methods

MethodDescription
Select()Select this range on the worksheet
Merge()Merge the range into a single cell
Unmerge()Unmerge the range
GroupRows()Group all rows in the range
GroupColumns()Group all columns in the range
UngroupRows()Ungroup all rows in the range
UngroupColumns()Ungroup all columns in the range
Contains(CellPosition pos)Check if a cell is within the range
Contains(RangePosition range)Check if a range is entirely contained
Contains(ReferenceRange range)Check if another reference range is contained
IntersectWith(RangePosition range)Check if two ranges overlap
IntersectWith(ReferenceRange range)Check if two reference ranges overlap
ToAddress()Convert to address string
ToAbsoluteAddress()Convert to absolute address string
IterateCells(iterator)Iterate through all cells in the range

Iterate Cells

var range = sheet.Ranges["A1:C3"];

range.IterateCells((row, col, cell) =>
{
    Console.WriteLine($"Cell [{row},{col}]: {cell?.Data}");
    return true;  // continue iterating
});

Convert Between Types

// RangePosition โ†’ ReferenceRange
var refRange = sheet.Ranges[rangePosition];

// ReferenceRange โ†’ RangePosition
RangePosition pos = refRange.Position;

// Implicit conversion (ReferenceRange to RangePosition)
RangePosition pos = refRange;

// Both work in assignments
sheet.SelectionRange = rangePosition;
sheet.SelectionRange = refRange;

Selection Range

Clear Range Content

Use ClearRangeContent to selectively clear elements from a range:

// Clear only data
sheet.ClearRangeContent("A1:B5", CellElementFlag.Data);

// Clear only formulas
sheet.ClearRangeContent("A1:B5", CellElementFlag.Formula);

// Clear everything
sheet.ClearRangeContent("A1:B5", CellElementFlag.All);

CellElementFlag Enum (Flags)

ValueDescription
DataCell data values
FormulaCell formulas
BodyCell bodies (buttons, checkboxes, etc.)
DataFormatData format settings
StyleCell styles
BorderCell borders
AllAll of the above

Used Range

Get the range that contains data:

RangePosition usedRange = sheet.UsedRange;

Named Range

Named ranges are special ReferenceRange instances with a name. See Named Range.

Was this article helpful?