Board index » delphi » Coloring Grid or Grid Fonts

Coloring Grid or Grid Fonts

    Could anyone please give me an example of how I might either color
particular rows in a grid or change the font color for particular rows in
Grid?  I need to do this based upon the dates provided by a TDateTime Field
in the dataset.  Records dated before a certain date I need to highlight in
the Grid in some way.
 

Re:Coloring Grid or Grid Fonts


Hi

This works for me placed in the 'OnDrawColumnCell' method of a DbGrid

 if DueItems.FieldByName('DueDate').AsDateTime<Calendar1.CalendarDate
then
    begin
    with dbgrid1 do begin
     Canvas.Brush.Color := clRed;        {Sets background color}
     Canvas.Font.Color := clwhite;            {Sets font color}
     DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;

Quote
"Robert Meek" <rme...@comcast.net> wrote in message

news:3f696c50$1@newsgroups.borland.com...
Quote
>     Could anyone please give me an example of how I might either color
> particular rows in a grid or change the font color for particular rows in
> Grid?  I need to do this based upon the dates provided by a TDateTime
Field
> in the dataset.  Records dated before a certain date I need to highlight
in
> the Grid in some way.

Re:Coloring Grid or Grid Fonts


Quote
Robert Meek wrote:
>     Could anyone please give me an example of how I might either color
> particular rows in a grid or change the font color for particular
> rows in Grid?  I need to do this based upon the dates provided by a
> TDateTime Field in the dataset.  Records dated before a certain date
> I need to highlight in the Grid in some way.

In the OnDrawColumnCell event of the grid you can test the field values via
the Column parameter, or you can test any field via the dataset.

If you want to do something only in that column (e.g. the date column) then:

  if Column.Field.AsDateTime < Date then
    DBGrid.Canvas.Font.Color := clRed
  else
    DBGrid.Canvas.Font.Color := clWindowText;

If you want the whole row highlighted:
  if ThatDataset.FieldByName('ThatDate').AsDateTime < Date then
    DBGrid.Canvas.Brush.Color := clRed
  else
    DBGrid.Canvas.Brush.Color := clWindow;

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson

Other Threads