Range

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

Ranges

In ReoGrid, ranges are represented through two primary objects:

  • Range Position: This is depicted by the RangePosition structure, which was referred to as ReoGridRange in versions prior to 1.1.0.
  • Range Instance: This is represented by the ReferenceRange class, providing 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 is designed to encapsulate the numerical information necessary to identify a specific range on a worksheet. It's important to note that RangePosition is not tied to any particular worksheet and does not store any data or style information. A RangePosition can be instantiated either 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
RowNumber of row of the start position
ColNumber of column of the start position
RowsNumber of rows contained in this range
ColsNumber of columns contained in this range
EndRowNumber of row of the end position
EndColNumber of column of the end position
IsEmptyCheck whether or not this range is empty

RangePosition contains the following methods:

range.Contains(CellPosition)          // check whether or not 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 range might out of the valid range of spreadsheet, to always get a safe range by using the FixRange method:

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

Range Instance

The range instance represents the range in a worksheet, always keeps the reference to a worksheet. When an associated worksheet is destroyed, all range instances belong to the worksheet will be invalid and should be destroyed.

Get a range instance

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

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

Access range data

It is possible to set range data by using Data property:

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

Set Range Data

Access styles of range

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

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

Access border of range

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

range.Border.Outside = RangeBorderStyle.BlackSolid;

Named range

Named range is a special range instance inherited from normal range instance, learn more about Named Range.

Convert between range position and range instance

Convert a range position to range instance:

var rangeInstance = sheet.Ranges[rangePosition];

Convert a range instance to range position:

var rangePos = rangeInstance.Position;

Range instance can convert into range position implicitly:

RangePosition rangePos = rangeInstance;

For example, the following all sentences are valid.

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

Selection Range

Used Range

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

var range = sheet.UsedRange;

Merge range

About merging range refer to Merge and Unmerge.


Was the content of the page helpful?