My goal is to draw a Delphi TDBgrid
cell to have a gradient color from right to left, like the image below:
I used the GradientFillCanvas
procedure from the Vcl.GraphUtil
unit.
Here is my custom OnDrawColumnCell
event handler:
procedure TMainForm.MyDBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
i : integer;
begin
if DM1.ABST_DV.RecNo mod 2=1 then
begin
GradientFillCanvas(MyDBGrid.Canvas,
clWhite,clGradientInactiveCaption,Rect, `gdHorizontal);
MyDBGrid.Canvas.Font.Color:=clBlack;
end else
begin
GradientFillCanvas(MyDBGrid.Canvas,
clWhite,clGradientInactiveCaption, Rect, gdHorizontal);
MyDBGrid.Canvas.Font.Color:=clBlack;
end;
end;
The problem is, with this code in the OnDrawColumnCell
event, the contents of the TDBGrid
do not appear.
here is my new code.
procedure TMainForm.MyDBGridDrawColumnCell(Sender: TObject; const
Rect: TRect; DataCol: Integer; Column: TColumn; State:
TGridDrawState); begin
GradientFillCanvas(MyDBGrid.Canvas,
clWhite,clGradientInactiveCaption,Rect, `gdHorizontal);
MyDBGrid.Canvas.Font.Color:=clBlack;
MyDBGrid.Canvas.TextRect(Rect,Column.Field.DisplayText); end;
but when i compile ann error occure and the message below is sent.
[DCC Erreur] uMain.pas(1349): E2250 Aucune version surchargée de ‘TextRect’ ne peut être appelée avec ces arguments
bedia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0
The content does not appear because you are not drawing it. You need to draw the cell’s text after drawing its background. You can use the MyDBGrid.Canvas.TextRect()
method for that purpose.
TDBGrid
does have a DefaultDrawDataCell()
method, which draws a cell and its text, and which can be customized using the OnDrawColumnCell
event, but it only supports drawing solid backgrounds, so it will draw over your custom background. You could try using a transparent Brush
to work around that, though.
4