Spread Windows Forms 17
Spread Windows Forms 17.0 Product Documentation / Developer's Guide / Cells / Allowing Cells to Merge Automatically
In This Topic
    Allowing Cells to Merge Automatically
    In This Topic

    You can have Spread automatically merge cells between columns or between rows if the cells have the same value based on the policy that you set. The component can automatically combine cells that have the same contents. You might want to do this, for example, when bound to a database, as shown in the figure below where the cells in the Year column merge where the year value is the same, as with 1995 and 2003.

    Spreadsheet with Merged Cells

    Unlike spanning cells, merging is an automatic feature. You tell the component which columns and rows allow cells to be combined automatically, and any cells within that set that have the same contents are combined for you.

    For more information on spanning cells, refer to Creating a Span of Cells.

    The policy you set determines how the component handles merging, as follows:

    You can have the cells in the specified row or column combine the cells automatically, or only combine them if the cells to their left (in columns) or above them (in rows) are merged. Typically, if you set the merge policy on several adjacent rows or columns, then you would use Always on the first row or column and Restricted on the remaining rows or columns.

    Merged cells take on the properties of the top-left merged cell. For example, if the top-left merged cell has a blue background color, the cells that merge with it display the same background color.

    Merged cells do not lose their data; it is simply hidden by the merge. If you remove the merge, the data appears in each cell that was in the merge. You can edit a cell that is merged with another cell. When you double-click the cell to turn edit mode on, the contents of the cell appear in the cell for you to edit them. When you leave edit mode, if the contents of the cell are no longer identical to the cell or cells with which it was previously merged, the cells are no longer displayed as merged.

    Cells that are different cell types but have the same contents can merge. For example, a date cell might contain the contents "01/31/02" and the adjacent edit cell might contain the same contents; if the column containing the cells is set to merge, the cells will merge. If the contents change or the merge is removed, the cells maintain their cell types as well as their data.

    To set cells to be merged if they have the same value, use the following members:

    For more information on these members, refer to the SheetView class (or the Row or Column class) or the DefaultSheetAxisModel of the FarPoint.Win.Spread.Model namespace.

    Using a Shortcut

    Use the SetColumMerge or SetRowMerge method for the Sheets or ActiveSheet shortcut object.


    This example code sets the row and column merge policies for all rows and all columns.

    Copy Code
    fpSpread1.Sheets[0].SetRowMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always);
    fpSpread1.Sheets[0].SetColumnMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always);
    Copy Code
    fpSpread1.Sheets(0).SetRowMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always)
    fpSpread1.Sheets(0).SetColumnMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always)

    Using Code

    Set the SetColumMerge or SetRowMerge method for a SheetView object.


    This example code merges columns and rows.

    Copy Code
    FarPoint.Win.Spread.SheetView Sheet0;
    Sheet0 = fpSpread1.Sheets[0];
    Sheet0.SetRowMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always);
    Sheet0.SetColumnMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always);
    Copy Code
    Dim Sheet0 As FarPoint.Win.Spread.SheetView
    Sheet0 = fpSpread1.Sheets(0)
    Sheet0.SetRowMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always)
    Sheet0.SetColumnMerge(-1, FarPoint.Win.Spread.Model.MergePolicy.Always)
    See Also