procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
with Sender as TDBGrid, DataSource.DataSet do
if (FieldByName('AmountPaid').AsFloat > 15000) then
DrawField(Column.Field.DisplayText, Rect, Canvas,
Column.Font, Column.Alignment, [fsBold],
clYellow, clRed);
end;
{This is the workhorse routine that does the drawing.}
procedure TForm1.DrawField(const Value : String;
const Rect : TRect;
vCanvas : TCanvas;
vFont: TFont;
vAlignment: TAlignment;
FontStyle : TFontStyles;
FontColor : TColor;
BGColor : TColor);
var
I : Integer;
begin
I := 0;
//First, fill in the background color of the cell
vCanvas.Brush.Color := BGColor;
vCanvas.FillRect(Rect);
//SetBkMode ensures that the background is transparent
SetBkMode(Canvas.Handle, TRANSPARENT);
//Set the passed font properties
vCanvas.Font := vFont;
vCanvas.Font.Color := FontColor;
vCanvas.Font.Style := vCanvas.Font.Style + FontStyle;
//Set Text Alignment
case vAlignment of
taRightJustify :
begin
SetTextAlign(vCanvas.Handle, TA_RIGHT);
I := Rect.Right - 2;
end;
taLeftJustify :
begin
SetTextAlign(vCanvas.Handle, TA_LEFT);
I := Rect.Left + 2;
end;
taCenter :
begin
SetTextAlign(vCanvas.Handle, TA_CENTER);
I := (Rect.Right + Rect.Left) DIV 2;
end;
end; { case }
//Draw the text
vCanvas.TextRect(Rect, I, Rect.Top + 2, Value);
SetTextAlign(vCanvas.Handle, TA_LEFT);
end;