Spread for ASP.NET 12 Product Documentation
Allowing Cells to Merge Automatically
Spread for ASP.NET 12 Product Documentation > Developer's Guide > Customizing the Appearance > Customizing the Appearance of a Cell > Allowing Cells to Merge Automatically

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.

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 Spanning Cells.

If the merge policy is set to None, cells within a row or column are not merged.

If the merge policy is set to Always, cells within a row or column are merged when the cells have the same values.

If the merge policy is set to Restricted, cells within a row or column are merged when the cells have the same values and the corresponding cells in the previous row or column also have the same value. For example, suppose cells A1:A8 contain {a; a; b; b; b; b; c; c} and cells B1:B8 contain {1; 1; 1; 1; 2; 2; 2; 2}. If the merge policy for column B is Always, the cells in column B are merged into two blocks B1:B4 and B5:B8. If the merge policy for column A is Always and the merge policy for column B is Restricted then the cells in column B are merged into four blocks B1:B2, B3:B4, B5:B6, and B7:B8. For example:

Restricted Merging

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 the top-left merged cell; when you leave edit mode, if the contents of that cell are no longer identical to the cell or cells with which it was previously merged, the cells are no longer displayed as merged when the Spread is updated. How cells are merged is only changed when the Spread is updated.

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 Model namespace in the Assembly Reference.

For more information on creating spans of cells with identical content, refer to Spanning Cells.

Using a Shortcut

Use the SetColumnMerge or SetRowMerge method for the FpSpread component Sheets shortcut.

Example

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

C#
Copy Code
FpSpread1.Sheets[0].SetRowMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always);
FpSpread1.Sheets[0].SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always);
VB
Copy Code
FpSpread1.Sheets(0).SetRowMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always)
FpSpread1.Sheets(0).SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always)

Using Code

Set the SetColumnMerge or SetRowMerge method for a SheetView object.

Example

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

C#
Copy Code
FarPoint.Web.Spread.SheetView Sheet0;
Sheet0 = fpSpread1.Sheets[0];
Sheet0.SetRowMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always);
Sheet0.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always);
VB
Copy Code
Dim Sheet0 As FarPoint.Web.Spread.SheetView
Sheet0 = fpSpread1.Sheets(0)
Sheet0.SetRowMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always)
Sheet0.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always)