AdvStringGrid GridGetCellColor

I get an error using this code.

The error is "Cell does not contain integer value".
The cells having values are integer, some cells are empty with no value.

This code should work?

procedure TfrmMyForm.GridGetCellColor(Sender: TObject; ARow,
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
begin
// Not if fixed row
if ARow>0 then
begin
// Not if empty
if grid.cells[ARow,5]<>'' then
// If value>0
if Grid.Ints[ARow,5]>0 then
ABrush.Color:=clred;
end;
end;

I can only suspect that OnGetCellColor is triggered for a cell not empty and not containing a number. I suggest to check the values returned by grid.Cells[].

You could also change this code to:
if TryStrToInt(grid.Cells[arow,5]) > 0 then ...

Does GridGetCellColor execute for each cell in a grid or only once for the whole grid?

I want to itterate through the grid and change the color in all cells in column 5 based on the cell value.

Is GridGetCellColor the event to do this?

OnGetCellColor is triggered whenever a cell needs to be painted or cell dimensions need to be calculated.

This code does not do what I thought it wold do.

Positive values in col 1 should have a black font color, negative values should have a red font color.

procedure TForm1.GridGetCellColor(Sender: TObject; ARow,
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
begin
if ACol=1 then
if Grid.Ints[ARow,1]<0 then
AFont.Color:=clred
else
AFont.Color:=clblack;
end;

Sample project with empty grid just to test above code. But values that are less than 0 does not have a red font color, the font color is still black even if I enter the value of -2 or other negative values.

Is GridGetCellColor the correct event to give negative values a red font color in a stringgrid? If not, what event should I use?

I have used 7-8 hours trying to get it work, without any success :hot_face: Sample code woud be great :smiley:

  1. grid values are accessed via Cells[Col,Row] or Ints[Col,Row] .
    I see you using ARow where I'd expect ACol?
  2. Sample 2 TMS Software | TAdvStringGrid Example demonstrates cell value dependent coloring

What a stupid mistake, [arow,acol] should be [acol,arow]

Thanks Bruno, works now.

I'm getting older. Must be age related :grin:

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.