I'm using Delphi XE5 and am trying to format a TGrid based on the contents of each cell. For numerics I'd like them right-aligned in the cell with negatives shown in red font. All other data should be left-aligned in the cell. The following code achieves this EXCEPT when I scroll the grid up or down the colour/alignment goes wrong.
type
TMyColumn = class( TStringColumn )
end;
procedure TForm1.Grid1GetValue(Sender: TObject; const Col, Row: Integer;
var Value: TValue);
var
vMyCell : TStyledControl;
i : Integer;
s : String;
begin
s := gHoldingGrid.Cells[ Col, Row ];
vMyCell := TMyColumn( Grid1.Columns[ Col ] ).CellControlByRow( Row );
if ( ( vMyCell <> nil ) AND ( vMyCell is TTextCell ) )
then begin
TTextCell( vMyCell ).StyledSettings := [];
if TryStrToInt( s, i )
then begin
if StrToInt( s ) < 0
then TTextCell( vMyCell ).FontColor := claRed
else TTextCell( vMyCell ).FontColor := claBlue;
TTextCell( vMyCell ).TextAlign := TTextAlign.taTrailing;
end { if TryStrToInt( s, i ) }
else begin
TTextCell( vMyCell ).TextAlign := TTextAlign.taLeading;
TTextCell( vMyCell ).FontColor := claGreen;
end; { else .... if TryStrToInt( s, i ) }
vMyCell.ApplyStyleLookup;
end; { if ( ( vMyCell <> nil ) AND ( vMyCell is TTextCell ) ) }
Value := s;
end;
Can someone help me solve this please? I've tried numerous examples on this forum but couldn't get them working and am well and truly stuck.
Many thanks in anticipation.