Spread for ASP.NET 12 Product Documentation
Understanding the Style Model
Spread for ASP.NET 12 Product Documentation > Developer's Guide > Using Sheet Models > Understanding the Models > Understanding the Style Model

The style model includes appearance settings, whether determined in the Spread Designer, or set as properties in the Properties List or as inherited by a custom skin for a whole sheet or by a custom style for individual cells. For more information on appearance settings for a sheet, refer to Creating a Skin for Sheets and Applying a Skin to a Sheet. For more information on appearance settings for a cell, refer to Creating and Applying a Custom Style for Cells.

Overview

The appearance settings may be set from any of the following classes in the Spread namespace that represent shortcut objects:

They also may be set from any of these classes in the Spread namespace that affect style:

The order of inheritance is described in Object Parentage. Here is a list of the style properties that are included in the style model, which are basically the members of the StyleInfo class, and affect the appearance or style of a cell:

The style model includes the cell types as well. The various cell types determine the appearance of a cell in several ways. For more information about the various cell types, refer to Customizing with Cell Types.

To use the underlying style model, use the methods of the style model for that sheet, specifically the GetDirectInfo method and SetDirectInfo method, and the settings in the StyleInfo object. Refer to the Assembly Reference for more information on the style model in general and to the DefaultSheetStyleModel methods in particular.

Direct Versus Composite

“Direct” in the style model means “not composite” or “not inherited.” SetDirectInfo sets the style properties that have been set for the specified cell, column, or row directly and does not return any settings that are set for higher levels (such as the entire model), while GetCompositeInfo gives the style properties "composed" or "merged" into one StyleInfo object that contains all the settings that are used to paint and edit the cell, column, or row, including any inherited settings.

Styles

Properties that correspond to StyleInfo properties are stored in the style model through the ISheetStyleModel interface. Style properties can be set for a cell, row (column index set to -1), column (row index set to -1), or the entire model (column and row index set to -1). Properties that are not set in a cell are inherited from the row setting, or the column setting if the row has no setting, or the model default if the column also has no setting. The default is exposed through the DefaultStyle property (SheetView.DefaultStyle, ColumnHeader.DefaultStyle, and RowHeader.DefaultStyle).

If you set or get a style property using Rows.Default or Rows[-1] or Columns.Default or Columns[-1], then you will actually be setting or getting the DefaultStyle property. This is because Column and Row always use a row index of -1 and a column index of -1 when accessing the style model, respectively, and so using a column index or a row index of -1 will be setting or getting the model default.

Setting StyleName replaces the style in the style model with the NamedStyle having the specified name. This has the effect of changing the settings of all of the style-related properties, including ParentStyleName (which wraps StyleInfo.Parent). Any previous setting for style-related properties like BackColor, Font, Border, or ParentStyleName are overwritten when you set StyleName. The component expects that the named style is set up with all the properties as you want them to be set. If this includes the named style having a parent NamedStyle, then you should have that parent set already when you assign it with StyleName, or you should assign it separately using a reference to the NamedStyle object (this has the same effect as setting ParentStyleName after setting StyleName). Keep in mind that after you have set StyleName, all cells where you have used that name are sharing the same NamedStyle object, and any changes that you make to one of those cells will also be changing all of the other cells sharing the same named style.

Example

The style properties for a cell can be composited or merged from the Cell, Row, Column, Sheet, and parent NamedStyle objects. This code snippet illustrates the order of inheritance of a composited style:

CS
Copy Code
NamedStyle test_parent = new NamedStyle("test_parent");
NamedStyle test = new NamedStyle("test", "test parent");
test_parent.BackColor = Color.Red;
test.ForeColor = Color.White;
FpSpread1.NamedStyles.AddRange(new NamedStyle[] {test_parent, test});
FpSpread1.Sheets(0).Columns(0).BackColor = Color.Blue;
FpSpread1.Sheets(0).Rows(0).CellType = new NumberCellType();
FpSpread1.Sheets(0).Cells(0,0).StyleName = "test";
FpSpread1.Sheets(0).Cells(1,0).StyleName = "test";

This code creates two NamedStyle objects with a parent-child relationship, then sets some properties on the styles and adds them to the NamedStyleCollection in the FpSpread component. Then the background color for the first column is set to blue and the cell type for the first row is set to Number, and the first cells in the first two rows are both set to use the NamedStyle named “test.” The result is that the cells in the first column are blue, except for the cells in the first two rows, which are red because the “test” style inherits the red background color from its parent NamedStyle. The parent style overrides the inherited setting for the column.

The cell type for the first cell is Number, since there is no cell type set in either NamedStyle object. There is a cell type set in the first row which is inherited by all cells in the row. The cell type for the second cell is General since there is no cell type setting for the cell, row, or column. The default cell type for the sheet is General. For more information on inheritance of style settings, refer to Object Parentage.

Formatting Information

The FormatInfo strings in a saved XML file are DateTimeFormatInfo or NumberFormatInfo objects that store the format of the data. These are created in the style model when a General cell is edited, if the style model implements IParseFormatSupport. These format objects allow the cells to display the data in the same format that was used to enter it. The General cell type parses the string into a number or DateTime, and generates the IFormatProvider and format string necessary to render the data as it was entered. If you use TextCellType instead of GeneralCellType, then it works the same as the edit cells did in the ActiveX Spread, and no FormatInfo is stored in the style model, but the data entered into the cells is always treated as text.