Board index » delphi » Please Help: DBGrid - change color of row

Please Help: DBGrid - change color of row

Delphi newbie:

I want to change the color of an entire row in DBGrid depending on cell
content.

E.g. if the Rows date ist todays date, change bkcolor of this row to
red. Is there a way to let a row flash?

Thanks,

Till

  vcard.vcf
< 1K Download
 

Re:Please Help: DBGrid - change color of row


Till -

You can use the OnDrawDataCell event handler to control the color of a
particular row if you are willing to invest the time and energy to do
so.

As for flashing, you'd need to set a timer and each time it "clicks",
invalidate the current row of the TDBGrid. The timer obviously needs
to keep track of the last color used, and switch appropriately upon
each click.

btw, you can save yourself some money by posting in plain Ascii format
and omitting the vcard. You will also save money for those who read
your messages.

We would also appreciate it if you did not "spam" the newsgroups. Post
to one group only. Thanks.

Good luck.

Kurt

Re:Please Help: DBGrid - change color of row


Till Coenen wrote in <362E64DD.253F4...@ba-ravensburg.de>...

Quote
>Delphi newbie:
>I want to change the color of an entire row in DBGrid depending on cell
>content.
>E.g. if the Rows date ist todays date, change bkcolor of this row to
>red.

These are some Delphi 2,3,4 examples:
====================================

Give a Record a new Color in a DBGrid:
--------------------------------------
PROCEDURE TForm1.DBGrid2DrawColumnCell(Sender: TObject; CONST Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
BEGIN
  IF (Table1.FieldByName('ToPay').asInteger>1000) THEN BEGIN
    DBGrid2.Canvas.Font.Color:= clRed;
    DBGrid2.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  END;
END;

Give a column in a DBGrid another Font (Bold):
---------------------------------------------
PROCEDURE TForm1.DBGrid3DrawColumnCell(Sender: TObject; CONST Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
BEGIN
  IF Column.FieldName='Firm' THEN BEGIN
    DBGrid3.Canvas.Font.Style:=DBGrid3.Canvas.Font.Style + [fsBold];
    DBGrid3.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  END;
END;

Change the color of a cell:
---------------------------
PROCEDURE TForm1.DBGrid4DrawColumnCell(Sender: TObject; CONST Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
BEGIN
  IF Column.FieldName='Adress' THEN
  IF Table1.FieldByName('Land').asString<>'US' THEN BEGIN
    DBGrid4.Canvas.Font.Color:= clBlue;
    DBGrid4.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  END;
END;

Background of a Field:
---------------------
PROCEDURE TForm1.DBGrid5DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
BEGIN
  IF Column.FieldName='Nr' THEN
  IF Odd(Column.Field.asInteger) THEN BEGIN
    DBGrid5.Canvas.Brush.Color:=clGreen
    DBGrid5.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  END;
END;

Other Threads