TGrid (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code example illustrates how cell values of a FireMonkey TGrid are assigned at run time.

Code

...
TForm1 = class(TForm)
  Grid1: TGrid;
  Column1: TColumn;
  CheckColumn1: TCheckColumn;
  StringColumn1: TStringColumn;
...
  procedure Button2Click(Sender: TObject);
  procedure Grid1SetValue(Sender: TObject; const Col, Row: Integer; const Value: TValue);
  procedure Grid1GetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue);
private
  A: array of TValue;
public
  constructor Create(AOwner: TComponent); override;
end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.fmx}
 
constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  SetLength(A, Grid1.RowCount);
end;
 
procedure TForm1.Grid1GetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue);
begin
  // Gets the value from a cell in the first column
  if Col = 0 then
    Value := A[Row];
end;
 
procedure TForm1.Grid1SetValue(Sender: TObject; const Col, Row: Integer; const Value: TValue);
begin
  // Sets the value for a cell in the first column
  if Col = 0 then
    A[Row] := Value;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  // Change values in the data array
  A[0] := 'Ok';
  A[1] := True;
  A[2] := 1234;
  A[3] := Pi;
  // Updates data in the grid cells
  Grid1.UpdateColumns;
end;
...

Uses