Board index » delphi » Changing font color of DbGrid cell.

Changing font color of DbGrid cell.

I have a DbGrid showing a database of products.  In one of the columns the
current stock is displayed. I want to color the font red if the number of
items on stock is below a certain value, so it attracts the users attention.

I wrote an OnDrawColumnCell event to handle it:

     begin
       if (StrToFloat(DbGrid1.Columns[5].field.displaytext))<50 then
           DbGrid1.Columns[5].font.color:=clRed
       else
           DbGrid1.Columns[5].font.color:=clBlack;
     end;

But it is no good, the column flickers and the coloring happens always one
row to late!

Here are my 2 questions:

1) Is DbGrid1.Columns.Field.DisplayText the correct way to querry the value?
(It is a calculated field I want to check, so DbGrid1.Fields[n] is out the
question, the GetFieldValue(n) works only with TCustomDbGrid.)

2) Is the OnDrawColumnCell event the right place to do the checking and
coloring?
Hopefully someone out there can help me, I would really appreciate it!

 

Re:Changing font color of DbGrid cell.


Quote
Alex Blankendaal wrote:
> I have a DbGrid showing a database of products.  In one of the columns the
> current stock is displayed. I want to color the font red if the number of
> items on stock is below a certain value, so it attracts the users attention.

> I wrote an OnDrawColumnCell event to handle it:

>      begin
>        if (StrToFloat(DbGrid1.Columns[5].field.displaytext))<50 then
>            DbGrid1.Columns[5].font.color:=clRed
>        else
>            DbGrid1.Columns[5].font.color:=clBlack;
>      end;

> But it is no good, the column flickers and the coloring happens always one
> row to late!

> Here are my 2 questions:

> 1) Is DbGrid1.Columns.Field.DisplayText the correct way to querry the value?
> (It is a calculated field I want to check, so DbGrid1.Fields[n] is out the
> question, the GetFieldValue(n) works only with TCustomDbGrid.)

> 2) Is the OnDrawColumnCell event the right place to do the checking and
> coloring?
> Hopefully someone out there can help me, I would really appreciate it!

1) NO!! This instruction will trigger a new OnDrawColumnCell event and you could
get an endless recursion!

2) It is the right event to do the coloring.

Further you must set DefaultDrawing to false and call DefaultDrawColumnCell at
the end of the event handler. Your event handler should look like this:

     begin
       if (StrToFloat(DbGrid1.Columns[5].field.displaytext))<50 then
           DbGrid1.Canvas.font.color:=clRed
       else
           DbGrid1.Canvas.font.color:=clBlack;
        DefaultDrawColumnCell(Rect,DataCol,Column,State);
     end;

Re:Changing font color of DbGrid cell.


Hello !

Have you considered what would happen if user rearanges the columns in
DBGrid ?

Re:Changing font color of DbGrid cell.


On Mon, 23 Feb 1998 22:37:24 +0100, Juergen Hemelt

Quote
<JHem...@metronet.de> wrote:
>Further you must set DefaultDrawing to false and call DefaultDrawColumnCell at
>the end of the event handler. Your event handler should look like this:

>     begin
>       if (StrToFloat(DbGrid1.Columns[5].field.displaytext))<50 then
>           DbGrid1.Canvas.font.color:=clRed
>       else
>           DbGrid1.Canvas.font.color:=clBlack;
>        DefaultDrawColumnCell(Rect,DataCol,Column,State);
>     end;

a better way would be to do this:

begin
  if (Column.FieldName = 'Inventory') then
    if (Column.Field.AsFloat < 50) then
      (Sender as TDBGrid).Canvas.Font.Color := clRed;

  DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

or if you want the whole row to be colored red then

begin
  if (Column.Field.DataSet.FieldByName('Inventory').AsFloat < 50) then
    (Sender as TDBGrid).Canvas.Font.Color := clRed;

  DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

-Shad

Other Threads