Ranges in ReoGrid are defined by a starting position and an ending position, with the smallest possible range encompassing at least one cell. Note that a range is not itself a merged cell, although it can be merged into a single cell. It is also possible for two ranges to intersect, as illustrated below:

Ranges

In ReoGrid, ranges are represented through two primary objects:

  • Range Position: Represented by the RangePosition structure (referred to as ReoGridRange in versions prior to 1.1.0).
  • Range Instance: Represented by the ReferenceRange class, which provides a more detailed and manipulable representation of a range within the worksheet.

These objects facilitate the management and manipulation of cell ranges, enabling complex operations and interactions within the spreadsheet.

Range Position

The RangePosition structure in ReoGrid encapsulates the numerical information needed to identify a specific range on a worksheet. Note that RangePosition is not tied to any particular worksheet and does not store any data or style information. A RangePosition can be instantiated from an address string or by specifying the starting position and size of the range, including:

  • The row index of the starting position (zero-based).
  • The column index of the starting position (zero-based).
  • The total number of rows in the range.
  • The total number of columns in the range.

Here are examples of how to create a RangePosition:

// Creating a range position from an address string
var rangeFromString = new RangePosition("C5:H14");

// Creating a range position by specifying the start position, number of rows, and number of columns
var rangeFromIndices = new RangePosition(4, 2, 9, 5);

Range

Properties and methods

Properties of RangePosition are used to get information from a range position:

PropertyDescription
RowRow index of the start position
ColColumn index of the start position
RowsNumber of rows contained in this range
ColsNumber of columns contained in this range
EndRowRow index of the end position
EndColColumn index of the end position
IsEmptyCheck whether this range is empty

RangePosition contains the following methods:

range.Contains(CellPosition)          // check whether the range contains a specified cell position
range.Equals(RangePosition)           // compare to another range
range.Offset(int rows, int cols)      // move range by n rows and n columns

Get a safe range position

Sometimes a range may be outside the valid bounds of a spreadsheet. To always get a valid range, use the FixRange method:

var fixedRange = sheet.FixRange(range);   // get a fixed range

Range Instance

The range instance represents a range in a worksheet and always holds a reference to that worksheet. When the associated worksheet is destroyed, all range instances belonging to it become invalid and should be discarded.

Get a range instance

To get a range instance, use the Ranges property of the worksheet.

var range = worksheet.Ranges["B2:D3"];

Access range data

You can set range data using the Data property:

range.Data = new object[] { "Product", "Unit Price", "Quantity", "Extended Price" };

Set Range Data

Access styles of range

To get or set styles on a range, use the Style property:

range.Style.BackColor = System.Drawing.Color.LightBlue;

Access border of range

To get or set borders on a range, use the Border property:

range.Border.Outside = RangeBorderStyle.BlackSolid;

Named range

A named range is a special range instance that inherits from a normal range instance. Learn more about Named Range.

Convert between range position and range instance

Convert a range position to a range instance:

var rangeInstance = sheet.Ranges[rangePosition];

Convert a range instance to a range position:

var rangePos = rangeInstance.Position;

A range instance can be converted to a range position implicitly:

RangePosition rangePos = rangeInstance;

For example, all of the following statements are valid:

sheet.SelectionRange = rangePosition;
sheet.SelectionRange = rangeInstance;

Selection Range

Used Range

To get the range that contains data in a worksheet, use the UsedRange property.

var range = sheet.UsedRange;

Merge range

For information on merging ranges, see Merge and Unmerge.

Was this article helpful?