Board index » delphi » Row Color in DBRID

Row Color in DBRID

Help needed...

How do I highlight a row in DBGRID.  For example I would like to have
every row highligted in green if the person age is > 20 in a particular
record.  I have the below code but it only highlight the AGE column
instead the whole row..which typically contains the person's NAME,
ADDRESS, etc..

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

var
  holdColor: TColor;

begin
  holdColor := DBGrid1.Canvas.Brush.Color; {store the original color}
  if  (Column.FieldName = 'AGE') and (Column.Field.AsInteger > 20) then
     begin
      DBGrid1.Canvas.Brush.Color := clGreen;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      DBGrid1.Canvas.Brush.Color := holdColor;
     end;

Thanks for appreciated anyone can shed some lights into this...

Regards,
Bert.

 

Re:Row Color in DBRID


Quote
>Norbert wrote:

> How do I highlight a row in DBGRID.  For example I would like to have
> every row highligted in green if the person age is > 20 in a particular
> record.  I have the below code but it only highlight the AGE column
> instead the whole row..which typically contains the person's NAME,
> ADDRESS, etc..

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:
     TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Table1.FieldByName('AGE').AsInteger > 20 then
    begin
      DBGrid1.Canvas.Font.Color := clGreen;       { Font color }
      DBGrid1.Canvas.Brush.Color := clWhite       { Cell color }
    end;
  DefaultDrawColumnCell(Rect, DataCol, Column, State)
end;

No need for holdColor if you don't want to change the background color.
Simply drop the Brush.Color line from the above code.
________
Andreas
Delphi 5.01 Pro, NT4 Sp6a
Direct replies mailto:paw...@gmx.net

Re:Row Color in DBRID


This works great and thank you except you have missed out DBGrid1 in
DefaultDrawColumnCell.  Any way now works great !

Bert.

Other Threads