Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DBGrid : Changing Cell and Font Colors at runtime

2,342 views
Skip to first unread message

Johannes de Wet

unread,
Nov 11, 1997, 3:00:00 AM11/11/97
to

We're currently using OnDrawColumnCell to change the color of the text and
background in a cell according to it's value (eg. if the cell value is < 0,
Font.color = clRed, otherwise Green).
The following code is used :

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin

//If column PROFIT's value is > 0 , make that cell Blue on Yellow,
otherwise Red on White
//This applies to the whole PROFIT column.

If column.FieldName = 'PROFIT' then
begin
if column.Field.AsInteger > 0 then
begin
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.Canvas.Brush.Color := clYellow;
DbGrid1.Canvas.FillRect(rect);
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end
else
begin
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.Canvas.Brush.Color := clWhite;
DbGrid1.Canvas.FillRect(rect);
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;

end;


How is it possible to change the Cell and Font Color of a different Column
other than the Cell currently being drawn ?

Any help appreciated!!

Johannes de Wet

unread,
Nov 11, 1997, 3:00:00 AM11/11/97
to


I don't think I stated my question clearly :
What I want to do is change the Cell and Font Color for eg. the 'Plant
Description' column if the value in the 'Profit' column is < 0. (on the
same row)

Johannes

Charles Lam

unread,
Nov 11, 1997, 3:00:00 AM11/11/97
to

Johannes,

your row in your query/table is still the same row regardless of the cell
you are drawing. The condition that exists in your data is true for all
cells of that row.

for instance:

balance is < 0. This is true when you are drawing for cell 1 or the
balance cell or what ever.

When the grid draws subsequent rows, the balance field for those rows will
change each time the row changes because it is tied to the DataSource.

I hope this helps!

Charles Lam

Kurt Barthelmess (TeamB)

unread,
Nov 12, 1997, 3:00:00 AM11/12/97
to

Johannes -

> How is it possible to change the Cell and Font Color of a different Column
> other than the Cell currently being drawn ?

In general, that is not practical. While you could redraw the other
cell directly, your drawing would only last until that cell was told
to redraw itself.

What you need to do is have some way to tell that other cell that it
should be drawn differently - and then ask it to do so by invalidating
that area of the grid.

Good luck.

Kurt

sharonsc...@gmail.com

unread,
Jan 29, 2018, 2:37:41 AM1/29/18
to
Any News On This?
0 new messages