Range

The range specified by a start position and an end position, the smallest range must contain at least one cell. Range is not a merged cell, but it can be merged as a single cell. Two ranges can be intersected.

Ranges

In ReoGrid, there are two objects about range:

  • Range Position, represented by structure RangePosition (called ReoGridRange before 1.1.0)
  • Range Instance, represented by class ReferenceRange

Range Position

Structure RangePosition contains the numeric information to locate a range on worksheet. RangePosition doesn’t associate with any worksheet, doesn’t keep data and styles. RangePosition can be created from address string or positions and size:

  • Number of row of the start position
  • Number of column of the start position
  • Number of rows
  • Number of columns

The number of rows, number of columns is zero-based index refers to the position on the worksheet.

// or create range position from address string
var range = new RangePosition("C5:H14");

// create range position at position 4, 2, contains 9 rows and 5 columns
var range = new RangePosition(4, 2, 9, 5);

Range

Property and method

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

Property Desc
Row Number of row of the start position
Col Number of column of the start position
Rows Number of rows contained in this range
Cols Number of columns contained in this range
EndRow Number of row of the end position
EndCol Number of column of the end position
IsEmpty Check 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" };

Result:

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;

Result:

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.

3 Responses to “Range”

  1. Polycarp says:

    Hi, am using vb.net and running into trouble when trying to use ReoGridRange? Help with this.

  2. zzy says:

    How can I get the column or row headers selected?