Back to Devexpress

Worksheet.MergeCells(CellRange) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-mergecells-x28-devexpress-dot-spreadsheet-dot-cellrange-x29.md

latest8.0 KB
Original Source

Worksheet.MergeCells(CellRange) Method

Merges the specified cells.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void MergeCells(
    CellRange range
)
vb
Sub MergeCells(
    range As CellRange
)

Parameters

NameTypeDescription
rangeCellRange

A CellRange object that specifies a range of cells to be merged.

|

Remarks

To access a cell obtained as a result of merging a cell range, use a cell reference for the top left cell of the original range or a reference for any range with this top left cell within the original range.

To unmerge cells, use the Worksheet.UnMergeCells method.

Another way to merge and unmerge a cell range is to use the range’s RangeExtensions.Merge and RangeExtensions.UnMerge extension methods, defined by the RangeExtensions class. These extension methods are accessible as methods of the CellRange object and called by using the instance method syntax.

Example

This example demonstrates how to merge several cells into a single cell. To do this, use the Worksheet.MergeCells method with the passed range of cells to be merged.

Note

When you merge a cell range, the data (value, formula and format settings) of only the top left non-empty cell will appear in the resulting merged cell. The data contained in other cells of the original range will be lost.

If all cells within the original range are empty, the range’s top left cell format will be applied to the merged cell.

View Example

csharp
// Merge cells contained in the range.
worksheet.MergeCells(worksheet.Range["A1:C5"]);
vb
' Merge cells contained in the range.
worksheet.MergeCells(worksheet.Range("A1:C5"))

The following code snippets (auto-collected from DevExpress Examples) contain references to the MergeCells(CellRange) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/FormulaActions.cs#L96

csharp
CellRange dataRangeHeader = worksheet.Range["A1:C1"];
worksheet.MergeCells(dataRangeHeader);
dataRangeHeader.Value = "myRange:";

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/FormulaActions.cs#L96

csharp
CellRange dataRangeHeader = worksheet.Range["A1:C1"];
worksheet.MergeCells(dataRangeHeader);
dataRangeHeader.Value = "myRange:";

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/FormulaActions.cs#L77

csharp
CellRange dataRangeHeader = worksheet.Range["A1:C1"];
worksheet.MergeCells(dataRangeHeader);
dataRangeHeader.Value = "myRange:";

web-dashboard-custom-info-exported-excel/CS/WebDashboardForm.aspx.cs#L43

csharp
sheet.MergeCells(sheet.Range.FromLTRB(5, 0, 8, 0));
Formatting formatting = textCell.BeginUpdateFormatting();

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/FormulaActions.vb#L66

vb
Dim dataRangeHeader As CellRange = worksheet.Range("A1:C1")
worksheet.MergeCells(dataRangeHeader)
dataRangeHeader.Value = "myRange:"

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/FormulaActions.vb#L85

vb
Dim dataRangeHeader As CellRange = worksheet.Range("A1:C1")
worksheet.MergeCells(dataRangeHeader)
dataRangeHeader.Value = "myRange:"

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/FormulaActions.vb#L87

vb
Dim dataRangeHeader As CellRange = worksheet.Range("A1:C1")
worksheet.MergeCells(dataRangeHeader)
dataRangeHeader.Value = "myRange:"

winforms-dashboard-customize-exported-document/VB/CustomExportDocumentExample/Form1.vb#L52

vb
sheet.MergeCells(sheet.Range.FromLTRB(5, 0, 8, 0))
Dim formatting As Formatting = textCell.BeginUpdateFormatting()

web-dashboard-custom-info-exported-excel/VB/WebDashboardForm.aspx.vb#L45

vb
textCell.Value = "Custom Document Header"
sheet.MergeCells(sheet.Range.FromLTRB(5, 0, 8, 0))
Dim formatting As Formatting = textCell.BeginUpdateFormatting()

See Also

IsMerged

GetMergedRanges()

How to: Merge Cells or Split Merged Cells

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace