Board index » delphi » How to change DBGrid text color in a single cell

How to change DBGrid text color in a single cell

Hi everyone!
Does someone know how to change the textcolor
of a single cell in a DBGrid ?

Thanks in advance

        Juergen

EMail: just...@rz-online.de

 

Re:How to change DBGrid text color in a single cell


Perhaps you can use this example,
Depending on the True or false value of the field, it colors the cell
red or green.  It uses the OnDrawColumnCell event.

procedure TFQWDK.DBGrid1DrawColumnCell(Sender: TObject; const Rect:  
        TRect;  DataCol: Integer; Column: TColumn; State:
           TGridDrawState);
  Var FieldValue: Boolean;
          lColor: Tcolor;
begin
  Try

    dbGrid1.Canvas.Pen.Color := clWhite;
    dbGrid1.Canvas.Brush.Color := clWhite;

    If DataCol = 8 then  //number of the collumn
    begin
      dbGrid1.Canvas.Rectangle(Rect.Left, Rect.Top, Rect.Right,

            Rect.Bottom);
      FieldValue := Column.Field.Value;

      If FieldValue  then  lColor := clGreen else lColor := clRed;
      dbGrid1.Canvas.Pen.Color := lColor;
      dbGrid1.Canvas.Brush.Color := lColor;
      dbGrid1.Canvas.Rectangle(Rect.Left + 3, Rect.Top + 3,
          Rect.Right - 3, Rect. Bottom - 3);
   end;

  except
    //do nothing
  end;

  end;
---------------------------------------------

I had to type this fast, so please check for obvious errors...

HTH, Eric

On Thu, 16 Apr 1998 20:36:09 +0200, Juergen Stein

Quote
<JuSt...@rz-online.de> wrote:
>Hi everyone!
>Does someone know how to change the textcolor
>of a single cell in a DBGrid ?

>Thanks in advance

>    Juergen

>EMail: just...@rz-online.de

Re:How to change DBGrid text color in a single cell


Hi Juergen,

Try something like this in the DBGridDrawColumCell event:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if (some_condition) AND (column.fieldname = 'column to be colored') then
    begin
      DBGrid1.Canvas.Font.Color := clRed;
      DBGrid1.Canvas.FillRect(Rect);
      DBGrid1.Canvas.TextRect(Rect, rect.left+3,rect.top+2,Column.Field.text);
    end
      else begin
         DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
     end;
end;

Regards,

Patrick

Regarding message <35364F99.4...@rz-online.de>
from <JuSt...@rz-online.de> on 16 Apr 98:

Quote

> Does someone know how to change the textcolor
> of a single cell in a DBGrid ?

Other Threads