FlexGrid for WinForms | ComponentOne
C1.Win.FlexGrid Namespace / CellStyle Class
Members Example

In This Topic
    CellStyle Class
    In This Topic
    The CellStyle class encapsulates properties that control the appearance of grid cells. This information includes the background and foreground colors, font, text and image alignment, etc.
    Object Model
    CellStyle Class
    Syntax
    'Declaration
     
    
    Public Class CellStyle 
    public class CellStyle 
    Remarks

    The C1FlexGridBase.Styles property exposes a collection of grid styles, and has methods for creating and removing styles from the grid.

    You can create and define styles at design time, but right-clicking the grid and selecting the "Edit Styles" menu option.

    Styles follow a hierarchical model similar to styles in Microsoft Word or in cascading style sheets. Every property in a CellStyle object may be left unassigned, in which case the value is inherited from a parent style. The parent style is usually the built-in CellStyleCollection.Normal style.

    To determine which elements are defined in a particular style, use the DefinedElements property.

    When you modify the properties of a CellStyle object, all cells that use that style are automatically repainted to reflect the changes.

    Styles may be assigned to CellRange, Row, and Column objects, as shown in the example below.

    Example
    The example below creates three CellStyle objects and assigns them to grid rows, columns, and cell ranges.

    After running this code, the third column of the grid will be painted with a red background. The third row will be painted with a green background. The cell at the intersection will be painted in red, because column styles have priority over row styles. The remaining style elements for these cells (font, alignment, etc) are not defined in the new styles and are automatically inherited from the parent style (CellStyleCollection.Normal).

    The cells around the intersection will have a bold font. The style that defines the bold font does not specify a background color, so that element is inherited from the parent style, which may be the "red", "green", or "normal" styles.

    // create style with red background
    CellStyle cs = _flex.Styles.Add("red");
    Style.BackColor = Color.Red;
                
    // create style with green background
    cs = _flex.Styles.Add("green");
    Style.BackColor = Color.Green;
                
    // create style with bold font
    cs = _flex.Styles.Add("bold");
    Style.Font = new Font("Tahoma", 8, FontStyle.Bold);
                
    // assign red style to a column
    _flex.Cols[3].Style = _flex.Styles["red"];
                
    // assign green style to a row
    _flex.Rows[3].Style = _flex.Styles["green"];
                
    // assign bold style to a cell range
    CellRange rg = _flex.GetCellRange(2, 2, 4, 4);
    rg.Style = _flex.Styles["bold"];
    Inheritance Hierarchy

    System.Object
       C1.Win.FlexGrid.CellStyle

    See Also