Board index » delphi » DBGrid : Changing Cell and Font Colors at runtime

DBGrid : Changing Cell and Font Colors at runtime

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!!

 

Re:DBGrid : Changing Cell and Font Colors at runtime


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

Re:DBGrid : Changing Cell and Font Colors at runtime


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

Re:DBGrid : Changing Cell and Font Colors at runtime


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

Other Threads