Stringgrid cell colour: yellow on red if negative?

Posted by Harry D. P16510 on July 18, 2008

In Reply to Re: stringgrid cell colour: red if negative posted by Jenny, DelphiLand Team on July 17, 2008

: Say that you want negative numbers in red on white. For this example, let's assume a stringgrid
: with the name "Grid", and that all its cells contain strings that represent valid numbers.
: ...
: procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
: Rect: TRect; State: TGridDrawState);
: var
: S: string; // string displayed in the cell
: begin
: S := Grid.Cells[ACol, ARow];
: if (Length(S) > 0) and (S[1] = '-') then begin
: // If first character is a minus sign
: Grid.Canvas.Font.Color := clRed;
: Grid.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, S);
: end;
: end;

How would I code it if I also want a different background in the cells? For example, yellow text on a red background for negative values?

How to limit this to certain columns and rows, for example only in the second column, without colouring the top row (top row used as fixed row for titles)?

Thanks a lot!

Related articles


Follow Ups