How to Use Styles > Applying Styles to Cells > 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, you can set style attributes which apply to any possible combination of cell status values. The AddCellStyle method is supported by the TDBGrid, TDBDropDown, Split, and Column objects, enabling you to control the range of cells for which certain conditions apply.
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:
Example Title |
Copy Code
|
---|---|
Dim S As New TrueDBGrid80.Style S.ForeColor = vbRed S.Font.Bold = True TDBGrid1.AddCellStyle dbgCurrentCell, 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.
You can also use styles defined at design time as arguments to the AddCellStyle method:
Example Title |
Copy Code
|
---|---|
Dim S As TrueDBGrid80.Style
Set S = TDBGrid1.Styles("RedBold")
TDBGrid1.AddCellStyle dbgCurrentCell, S
|
The preceding example can be simplified since the AddCellStyle method accepts a style name as well as an actual style object:
Example Title |
Copy Code
|
---|---|
TDBGrid1.AddCellStyle dbgCurrentCell, "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 dbgCurrentCell applies only to cells which have only this status. Thus, cells which are current but also updated (dbgCurrentCell + dbgUpdatedCell) will not be displayed in red and bold unless you also execute the following statement:
Example Title |
Copy Code
|
---|---|
TDBGrid1.AddCellStyle dbgCurrentCell + dbgUpdatedCell, "RedBold"
|
Note: The current cell status is only honored when the MarqueeStyle property is not set to 6 - Floating Editor. The floating editor marquee always uses the system highlight colors as determined by your 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.