Merge and Unmerge

To merge a range, use one of two ways below:

// get current active worksheet
var worksheet = reoGridControl1.CurrentWorksheet;

// way 1: call method of worksheet
worksheet.MergeRange("B3:E5");

// way 2: merge via range instance
var range = worksheet.Ranges["B3:E5"];
range.Merge();

22_2

To unmerge a range:

worksheet.UnmergeRange("B3:E5");

// or:
var range = worksheet.Ranges["B3:E5"];
range.Unmerge();

Undo & Redo & Repeat

Merge and Unmerge could be done by action, performing by action that allows the operation could be undone.

To merge a range by action:

worksheet.DoAction(new MergeRangeAction("B3:E5"));

To unmerge range by action:

worksheet.DoAction(new UnmergeRangeAction("B3:E5"));

Run Script

When using extended edition, script execution is available. To merge and unmerge by script:

var sheet = workbook.CurrentWorksheet;

sheet.mergeRange(new Range("B3:E5"));
sheet.unmergeRange(new Range("B3:E5"));

Example: Merge or unmerge current selected range

var sheet = workbook.CurrentWorksheet;

sheet.mergeRange(grid.selection);
sheet.unmergeRange(grid.selection);

Remark

UnmergeRange method will find and unmerge all merged cells in a specified range. For example, merged cells A, B and C below will be unmerged if the red range is passed as argument.

9_2

Merged Cells

Check for merged cell

Only the most top left cell in a merged range is the merged cell. Except the top left cell, other cells merged into range will become invalid.

10

To check whether or not a cell is merged cell, use method IsMergedCell of worksheet:

bool isMergedCell = worksheet.IsMergedCell(2, 1);      // false
bool isMergedCell = worksheet.IsMergedCell(2, 2);      // true
bool isMergedCell = worksheet.IsMergedCell(2, 3);      // false

Rowspan & Colspan

A merged cell has two properties RowSpan and ColSpan, what are used to determine how many cells are merged in each direction:

11 (1)

The B3 cell is a merged cell, its rowspan is 4 and colspan is 3.

By getting instance of the cell to get more information, to get rowspan and colspan:

var rowspan = cell.GetRowspan();   // number of rowspan
var colspan = cell.GetColspan();   // number of colspan

Property IsMergedCell is used to check whether a cell is merged cell:

var cell = worksheet.Cells["B3"];
bool isMergedCell = cell.IsMergedCell;  // true

Find out merged cell from a range

ReoGrid provides a method called GetMergedCellOfRange which is used to find merged cell from a specified range.

12 (1)

ReoGridCell output = worksheet.GetMergedCellOfRange(input);

Valid and invalid cells

Cells merged by other cell become invalid, an invalid cell cannot display data and styles. The below red range is a merged cell, except the first cell, other cells labeled No are invalid cells, they can't be selected and edited.

19 (1)

There is a method of worksheet named IsValidCell that is used to check whether or a cell is valid.

sheet.IsValidCell(2, 2);      // true
sheet.IsValidCell(2, 3);      // false

Check for whole merged cell

There is a method used to check and get a whole cell if part of one is given.

56

RangePosition outputRange = sheet.CheckMergedRange(inputRange);

RangeIntersectionException

Range to be merged may intersect with other merged cells, try to merge intersected range will cause RangeIntersectionException.

20 (2)

User code should handle this exception and cancel the user operation.

21 (1)

Method CheckIntersectedMergingRange used to check whether or not there is any a part of merged cell contained in specified range. This method is useful to avoid the above exception.

RangePosition outputRange = sheet.CheckIntersectedMergingRange(inputRange);

If outputRange.IsEmpty is true, that means the inputRange is safe. There is another method to do the same:

bool rs = sheet.HasIntersectedMergingRange(inputRange);

Was the content of the page helpful?