[]
        
(Showing Draft Content)

Merge Cells

FlexSheet for WPF allows you to merge cells in a situation where you need to combine the data in multiple cells into a single cell. For example, you want to create a project timeline worksheet where you can track your project deadlines and status, and want to give it a title heading as well. To create a title heading, you need to combine multiple cells into one, which can be achieved by merging cells.

Merging in C1FlexSheet can be performed using ExcelMergeManager class. The code given below is used to merge cells in C1FlexSheet. In our case, we have implemented this code on the click event of a button.

vbnet

' get current selection, ensure there's more than one cell in it
Dim sel = flex.Selection
Dim xmm = TryCast(flex.MergeManager, ExcelMergeManager)
If xmm IsNot Nothing Then
    ' check if the selection contains any merged ranges
    Dim hasMerges = False
    For Each cell As CellRange In sel.Cells
        If Not xmm.GetMergedRange(flex, CellType.Cell, cell).IsSingleCell Then
            hasMerges = True
        End If
    Next
    ' toggle merging for the selection
    If hasMerges Then
        ' clear merged ranges
        xmm.RemoveRange(sel)
    Else
        ' merge selection
        xmm.AddRange(sel)
    End If
    ' show changes
    flex.Invalidate()
End If

csharp

// get current selection, ensure there's more than one cell in it
var sel = flex.Selection;
var xmm = flex.MergeManager as ExcelMergeManager;
if (xmm != null)
{
    // check if the selection contains any merged ranges
    var hasMerges = false;
    foreach (var cell in sel.Cells)
    {
        if (!xmm.GetMergedRange(flex, CellType.Cell, cell).IsSingleCell)
        {
            hasMerges = true;
        }
    }
    // toggle merging for the selection
    if (hasMerges)
    {
        // clear merged ranges
        xmm.RemoveRange(sel);
    }
    else
    {
        // merge selection
        xmm.AddRange(sel);
    }
    // show changes
    flex.Invalidate();
}