I have a Firemonkey Desktop Application for Windows. I have a TGrid
which I populate through a visual livebinding TBindSourceDB
component. I want some columns to have text aligned to the right, as they are numerical values. I have tried through:
onPainting
eventTTextCell
control by number of ColumnxRow
TextAlignt
property to the rightNone of those measures align the text to the right. I have tried to set it at runtime, unsuccessfully though, getting the TStyledControl
and assigning procedures to the onApplyStyleLookup
of the TTextCell
.
Any ideas on it? The App runs, but nothing happens. The cell texts are still left aligned.
Use the OnDrawColumnCell Event.
For columns containing text cells, the text layout information for each individual Column is assigned from the TextSettings property of the grid. However, the assignment is performed prior to the event firing.
The best and simplest way is to just directly access the layout for a specific column via a class helper before any drawing takes place.
Set the DefaultDrawing property of the grid to False and paste the following code:
interface
type
TColumnHelper = class helper for FMX.Grid.TColumn
function getTextLayout: TTextLayout;
end;
implementation
{ TColumnHelper }
function TColumnHelper.getTextLayout: TTextLayout;
begin
Result := Self.FDrawLayout;
end;
{ OnDrawColumnCell }
procedure GridDrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
begin
{ change text layout info prior to default drawing }
if Column.Header = 'Numerical Column' then
Column.getTextLayout.HorizontalAlign := TTextAlign.Trailing
else
Column.getTextLayout.HorizontalAlign := TGrid(Sender).TextSettings.HorzAlign;
{ perform default drawing }
TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
end;