Cell Merging

Of course we could not call a grid C1FlexGrid if it didn't support cell merging.

The AllowMerging property on the grid enables cell merging at the grid level. Once you have enabled merging at the grid level, use the Row.AllowMerging and the Column.AllowMerging properties to select the specific rows and columns that should be merged.

For example, the code below causes the grid to merge cells that contain the same country:

// enable merging in the scrollable area
fg.AllowMerging = AllowMerging.Cells;

// on columns "Country" and "FirstName"
fg.Columns["Country"].AllowMerging = true;
fg.Columns["FirstName"].AllowMerging = true;

That is all it takes to create the grid shown below:

The default merging behavior consists of merging adjacent cells that have the same content.

You can customize this behavior to create custom merging modes by creating a class that implements the IMergeManager interface and assigning an instance of that class to the grid's MergeManager property.

The IMergeManager interface is very simple (it contains only one method):

public interface IMergeManager
{
  CellRange GetMergedRange(
    C1FlexGrid grid, CellType cellType, CellRange range);
}

The GetMergedRange method takes parameters that specify the owner grid, the type of cell (scrollable, column header, etc.), and the single-cell range that may be merged with adjacent cells. The return value is a CellRange object that contains the range passed in and any adjacent cells that should be merged with it.

Note that although the interface is simple, the actual implementation of this method can be fairly complicated. A custom implementation that returns overlapping ranges for example may cause the grid to enter an infinite loop. Also, inefficient implementations will hurt performance.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.