Board index » delphi » Highlight cell color in a dbgrid

Highlight cell color in a dbgrid

How do I change the Highlight cell color in a dbgrid
depending on values I want a negative# to be red
Positive Green
I only want to change the individual cell that is clicked on!
Looking for a small example code

Thanks to anyone who knows

 

Re:Highlight cell color in a dbgrid


I have this and a few other things on TDBGrid as a tip
on my Delphi Tip of the Day web page at:
http://members.truepath.com/delphi

I also have an email list.  Daily I send my tip title, tip description, and
a link to the tip if you are interested in it that day.
--
--
Lewis Howell
lewishow...@yahoo.com
Lou's Delphi Tip of the Day:
http://members.truepath.com/delphi
Lou's personal webpage:
http://members.truepath.com/LewisHowell

Quote
DHastas wrote in message <370848A7.E950A...@adelphia.net>...
>How do I change the Highlight cell color in a dbgrid
>depending on values I want a negative# to be red
>Positive Green
>I only want to change the individual cell that is clicked on!
>Looking for a small example code

>Thanks to anyone who knows

Re:Highlight cell color in a dbgrid


Quote
DHastas <dhas...@adelphia.net> wrote in message

news:370848A7.E950A5FB@adelphia.net...

Quote
> How do I change the Highlight cell color in a dbgrid
> depending on values I want a negative# to be red
> Positive Green
> I only want to change the individual cell that is clicked on!
> Looking for a small example code

You need to handle the OnDrawColumnCell event. In this event, the DataCol
parameter tells you which column index is being drawn, and the Column
parameter gives access to the field, State tells you how it should be drawn:

(the following works on the DBDEMOS Orders.DB table)

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (Column.FieldName = 'ItemsTotal')
  and (Column.Field.AsFloat > 10000) then
    DBGrid1.Canvas.Font.Color := clRed
  else
    DBGrid1.Canvas.Font.Color := DBGrid1.Font.Color;

  // let the grid do the rest of the work!
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

--
Wayne Niddery - WinWright Consulting
Delphi, C++Builder, JBuilder, InterDev --
http://home.ican.net/~wniddery/RADBooks.html
...remove chaff when replying...
"You know you've landed gear-up when it takes full power to taxi"

Re:Highlight cell color in a dbgrid


See the OnDrawDataCell event handler.

Good luck.

Kurt

Other Threads