ComponentOne True DBGrid for WinForms
How to Use Styles / Applying Styles to Cells / Applying Cell Styles by Status
In This Topic
    Applying Cell Styles by Status
    In This Topic

    Each cell in the True DBGrid for WinForms display has a status value which identifies its disposition (any combination of current, modified, part of a selected row, or part of a highlighted row). Using the AddCellStyle method, set style attributes that apply to any possible combination of cell status values. The AddCellStyle method is supported by the C1TrueDBGrid, C1TrueDBDropDown, Split, and C1DisplayColumn objects, enabling the range of cells for which certain conditions apply to be controlled.

    For each unique status combination, you can set the color, font, and picture attributes to be used for cells of that status. When a cell's status changes, True DBGrid for WinForms checks to see if any style property overrides are defined for that cell, and applies those attributes to the cell when it is displayed. Style objects are used to specify the color and font for a cell, as in the following example:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim S As New C1.Win.C1TrueDBGrid.Style()
    Dim myfont As Font
     
    myfont = New Font(S.Font, FontStyle.Bold)
    S.Font = myfont
    S.ForeColor = System.Drawing.Color.Red
    Me.C1TrueDBGrid1.AddCellStyle (C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, S)
    

    To write code in C#

    C#
    Copy Code
    C1TrueDBGrid.Style  S = new C1.Win.C1TrueDBGrid.Style();
    Font myfont;
     
    myfont = new Font(S.Font, FontStyle.Bold);
    S.Font = myfont;
    S.ForeColor = System.Drawing.Color.Red;
    this.c1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, S);
    

    Here, a new temporary style object is created to specify the color and font overrides (red text, bold) to be applied to the current cell throughout the entire grid. Since the style object's BackColor property is not set explicitly, the background color of the current cell is not changed.


    Also use styles defined at design time as arguments to the AddCellStyle method:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim S As C1.Win.C1TrueDBGrid.Style
    S = Me.C1TrueDBGrid1.Styles("RedBold")
    Me.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, S)
    

    To write code in C#

    C#
    Copy Code
    C1.Win.C1TrueDBGrid.Style S;
    S = this.c1TrueDBGrid1.Styles("RedBold")
    this.c1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, S);
    

    The preceding example can be simplified since the AddCellStyle method accepts a style name as well as an actual style object:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Me.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, "RedBold")
    

    To write code in C#

    C#
    Copy Code
    this.c1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, "RedBold");
    

    All of the preceding examples cause the text of the current cell to appear in red and bold. However, it is important to note that the status CellStyleFlag.CurrentCell applies only to cells that have only this status. Thus, cells that are current but also updated (CellStyleFlag.CurrentCell+CellStyleFlag.UpdatedCell) will not be displayed in red and bold unless the following statement is executed:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Me.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell + C1.Win.C1TrueDBGrid.CellStyleFlag.UpdatedCell, Me.C1TrueDBGrid1.Styles("RedBold"))
    

    To write code in C#

    C#
    Copy Code
    this.c1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell | C1.Win.C1TrueDBGrid.CellStyleFlag.UpdatedCell, this.c1TrueDBGrid1.Styles["RedBold"]);
    
    Note: The current cell status is only honored when the MarqueeStyle property is not set to MarqueeEnum.FloatingEditor. The floating editor marquee always uses the system highlight colors as determined by Control Panel settings.

    Although this method of specifying cell conditions offers more control and flexibility, it also requires that additional code be written for some common cases.

    Calls to AddCellStyle take effect immediately, and can be used for interactive effects as well as overall grid characteristics.

    See Also