I'm creating a derivative of the TDBGrid, and I want to implement a nicer way to define its text formatting, something similar to the GetContentStyle of the QuantumGrid.
The problem is that the DBGrid ignores the font and colors that my new event sets on its Canvas.
type TSetCellStyle = procedure(const Sender: TObject; const AColumn: TColumn; const ARow: TDataset; const AField: TField; const State: TGridDrawState; var TextFont: TFont; var BackgroundColor: TColor) of object;
TMyDBGrid = class(TDBGrid)
private
FSetCellStyle: TSetCellStyle;
protected
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); override;
published
property OnSetCellstyle: TSetCellStyle read FSetCellStyle write FSetCellStyle;
...
...
implementation
procedure TMyDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
BeginUpdate;
Canvas.Lock; // Prevents other threads from drawing on the canvas.
if Assigned(FSetCellStyle) then begin
var TextFont: TFont;
var BackgroundColor: TColor;
TextFont := Canvas.Font;
BackgroundColor := Canvas.Brush.Color;
FSetCellStyle(Self, Column, Self.DataSource.DataSet, Self.DataSource.DataSet.FindField(Column.FieldName), State, TextFont, BackgroundColor);
Canvas.Font := TextFont;
Canvas.Brush.Color := BackgroundColor;
end;
Canvas.Unlock;
inherited DrawColumnCell(Rect, DataCol, Column, State);
EndUpdate;
end;
This is an exemple of how the application uses the new event to customize the formatting of a grid:
procedure TFInspira.GridInspiraSetCellStyle(const Sender: TObject; const AColumn: TColumn; const ARow: TDataSet; const AField: TField; const State: TGridDrawState; var TextFont: TFont; var BackgroundColor: TColor);
begin
if (AColumn.FieldName = 'ReferenciaGrup') and (ARow.FieldByName('PrimerDeGrup').AsBoolean) then begin
BackgroundColor := clYellow;
end;
if ARow.FieldByName('Selected').AsBoolean then begin
TextFont.Style := TextFont.Style + [fsItalic];
end;
end;
I can debug my grid and see that the overriden DrawColumnCell sets the canvas in yellow and italic for some cells, but the Grid never shows them. Looks like the call to inherit DrawColumnCell
resets the Canvas' formats.
If I can't hook my formatting event in DrawColumnCell where can I do so ?.
Thank you.