Board index » delphi » Changing the text color for a specific cell in a TDBGrid

Changing the text color for a specific cell in a TDBGrid

Hi,

I am trying to find the way to change the color of the text in a TDBGrid
cell based on the content of the field used to populated the cell. For
example, in a column that shows a dollar figure, I would like to see the
value in red when the dollar figure is negative and blue when the figure is
positive. I assume there must be an easy way to accomplish this but can't
seem to find it. I tried the OnDrawDataCell event but it seems that
changing the color of the font in this event changes the color for the
entire grid; not just the cell being drawn.

Any suggestions will be appreciated.

Thank you,

Philippe Gingras <pging...@felitec.com>

 

Re:Changing the text color for a specific cell in a TDBGrid


I have never used a TDBGrid but if it behaves similar to TStringGrid
then try the
following code in OnDrawDataCell

procedure TfmSchedule.GridOnDrawCell(Sender: TObject; Col,
  Row: Integer; Rect: TRect; State: TGridDrawState);
var
  cv: TRect;
begin
  cv := (Sender is TStringGrid).CellRect(Col, Row);
  (Sender is TStringGrid).Canvas.Rectangle(cv.Left, cv.Top, cv.Right,
cv.Bottom);
  (Sender is TStringGrid).Canvas.Font.Color := clRed;
  {do other stuff ...}
end;

Quote
Philippe Gingras wrote:

> Hi,

> I am trying to find the way to change the color of the text in a TDBGrid
> cell based on the content of the field used to populated the cell. For
> example, in a column that shows a dollar figure, I would like to see the
> value in red when the dollar figure is negative and blue when the figure is
> positive. I assume there must be an easy way to accomplish this but can't
> seem to find it. I tried the OnDrawDataCell event but it seems that
> changing the color of the font in this event changes the color for the
> entire grid; not just the cell being drawn.

> Any suggestions will be appreciated.

> Thank you,

> Philippe Gingras <pging...@felitec.com>

Other Threads