ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Applying Cell Styles by Status

Each cell in the True DBGrid 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 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:

·      Visual Basic

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)

·      C#

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);

·      Delphi

var S: C1.Win.C1TrueDBGrid.Style;

    myfont: Font;

 

S := C1.Win.C1TrueDBGrid.C1TrueDBGrid.Style.Create;

myfont := Font(S.Font, FontStyle.Bold);

S.Font := myfont;

S.ForeColor := System.Drawing.Color.Red;

Self.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:

·      Visual Basic

Dim S As C1.Win.C1TrueDBGrid.Style

S = Me.C1TrueDBGrid1.Styles("RedBold")

Me.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, S)

·      C#

C1.Win.C1TrueDBGrid.Style S;

S = this.C1TrueDBGrid1.Styles("RedBold")

this.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, S);

·      Delphi

var S: C1.Win.C1TrueDBGrid.Style;

 

S := Self.C1TrueDBGrid1.Styles['RedBold'];

Self.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:

·      Visual Basic

Me.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, "RedBold")

·      C#

this.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell, "RedBold");

·      Delphi

Self.C1TrueDBGrid1.AddCellStyle(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:

·      Visual Basic

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

·      C#

this.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell | C1.Win.C1TrueDBGrid.CellStyleFlag.UpdatedCell, this.c1TrueDBGrid1.Styles["RedBold"]);

·      Delphi

Self.C1TrueDBGrid1.AddCellStyle(C1.Win.C1TrueDBGrid.CellStyleFlag.CurrentCell and C1,Win.C1TrueDBGrid.CellStyleFlag.UpdatedCell, Self.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.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.