Re: dbgrid cell color help

Posted by Freddy on July 09, 2009

In Reply to dbgrid cell color help posted by danielhp on May 29, 2009

: I want to create an application where some dbgrid cells in a column have a different background color. The color can be changed by changing RGB value.
: Can it be done?
: If you can give a sample code,it will be much appreciated.
: Thankyou.

Example: color the cells of column number 1 yellow on red, if the value in field "Year" is greater then 1960:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
  if (DataCol = 1) and (Table1.FieldByName('Year').AsInteger > 1960) then begin
    DBGrid1.Canvas.Font.Color  := clYellow;   // the text
    DBGrid1.Canvas.Brush.Color := clRed;      // the background
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);

If you only want to change the background color, omit the line with "DBGrid1.Canvas.Font.Color"

Good luck!

Follow Ups