TcxCustomGridTableViewStyles.OnGetContentStyle Event
Allows you to change the content style of individual cells depending on specific conditions in your application.
Declaration
property OnGetContentStyle: TcxGridGetCellStyleEvent read; write;
Remarks
Content, ContentEven, ContentOdd, HotTrack, Inactive, and Selection properties allow you to define styles for all content cells in different states.
You can handle OnGetContentStyle
, OnGetHotTrackStyle, OnGetInactiveStyle, and OnGetSelectionStyle events to change styles for individual cells in different states depending on cell values and other conditions in your application.
Event Occurrence
The OnGetContentStyle
event occurs every time the grid View is about to draw a cell in the normal (base) state.
Important
Since the OnGetContentStyle
event can be frequent, its handler should avoid time-consuming calculations to prevent slowing down the application’s UI.
If you need to rely on data-dependent conditions in an OnGetContentStyle
event handler, we recommend that you use only cached or pre-calculated values rather than complex calculations at the data controller or dataset level.
Event Parameters
The following parameters are available within an OnGetContentStyle
event handler:
Sender
- Provides access to the grid Table View that raised the cell style customization event.
ARecord
|AItem
Provide access to the record and data item that identify the processed cell.
These parameters return
nil
(in Delphi) ornullptr
(in C++Builder) if the currently processed cell belongs to a group row or another auxiliary grid View element.AStyle
Specifies the style applied to the currently processed cell.
Tip
We recommend that you use the TcxStyleRepository component to store and manage individual styles.
Refer to the TcxGridGetCellStyleEvent procedural type description for detailed information on all available options.
Code Example
The code example in this topic applies cxStyleGroup1 and cxStyleGroup2 styles to data rows whose cells contain Group1
and Group2
values.
Follow the steps below to test this code example in your RAD Studio IDE:
- Copy the DFM code snippet below.
- Create a new project in the IDE and focus an empty form.
- Press Ctrl+V to populate the form with preconfigured components.
- Expand the Styles node in the Events tab of the Object Inspector.
- Create an empty
OnGetContentStyle
event handler, paste the code example, and run the project.
object cxGrid1: TcxGrid
Left = 72
Top = 80
Width = 545
Height = 200
TabOrder = 0
object cxGrid1DBTableView1: TcxGridDBTableView
Navigator.Buttons.CustomButtons = <>
ScrollbarAnnotations.CustomAnnotations = <>
DataController.DataSource = DataSource1
DataController.Summary.DefaultGroupSummaryItems = <>
DataController.Summary.FooterSummaryItems = <>
DataController.Summary.SummaryGroups = <>
Styles.OnGetContentStyle = cxGrid1DBTableView1StylesGetContentStyle
object cxGrid1DBTableView1RecId: TcxGridDBColumn
DataBinding.FieldName = 'RecId'
Visible = False
end
object cxGrid1DBTableView1Groups: TcxGridDBColumn
DataBinding.FieldName = 'Groups'
end
object cxGrid1DBTableView1Names: TcxGridDBColumn
DataBinding.FieldName = 'Names'
end
object cxGrid1DBTableView1Values: TcxGridDBColumn
DataBinding.FieldName = 'Values'
Width = 54
end
end
object cxGrid1Level1: TcxGridLevel
GridView = cxGrid1DBTableView1
end
end
object dxMemData1: TdxMemData
Active = True
Indexes = <>
Persistent.Data = {
5665728FC2F5285C8FFE3F04000000140000000100070047726F757073001400
0000010006004E616D657300040000000300070056616C756573000400000009
000600446174657300010600000047726F75703101050000004E616D6531010A
000000017B000B00010600000047726F75703101050000004E616D6532011400
000001CF0E0B00010600000047726F75703201050000004E616D6533011E0000
00017A210B00010600000047726F75703201050000004E616D65340128000000
01892B0B00}
SortOptions = []
Left = 520
Top = 88
object dxMemData1Groups: TStringField
FieldName = 'Groups'
end
object dxMemData1Names: TStringField
FieldName = 'Names'
end
object dxMemData1Values: TIntegerField
FieldName = 'Values'
end
object dxMemData1Dates: TDateField
FieldName = 'Dates'
end
end
object DataSource1: TDataSource
DataSet = dxMemData1
Left = 472
Top = 176
end
object cxStyleRepository1: TcxStyleRepository
Left = 520
Top = 144
PixelsPerInch = 96
object cxStyleGroup1: TcxStyle
AssignedValues = [svColor]
Color = clRed
end
object cxStyleGroup2: TcxStyle
AssignedValues = [svColor]
Color = clLime
end
end
procedure TMyForm.cxGrid1DBTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
var
AValue: Variant;
begin
if ((ARecord <> nil) and (AItem <> nil)) then
begin
AValue := ARecord.Values[cxGrid1DBTableView1Groups.Index];
if AValue = 'Group1' then
AStyle := cxStyleGroup1
else if AValue = 'Group2' then
AStyle := cxStyleGroup2;
end;
end;
Important Limitations
- You can only assign a background bitmap for the entire grid View through the Content.Bitmap property. An
OnGetContentStyle
event handler cannot define different background bitmaps for individual cells. - Do not perform any operations at the dataset level within an
OnGetContentStyle
event handler. These operations may move focus and send other notifications that can trigger an infinite loop.