TStringGrid - FireMonkey Guide

Back to FireMonkey Library

Unit: FMX.Grid
Parent: TStyledControl via TCustomGrid

TStringGrid behaves in a similar manner to TGrid except that it can only contain/display strings and it stores those strings internally. Contrast this with TGrid which doesn’t store any data but fetches data as needed and passes out modified data via the OnGetValue and OnSetValue events respectively.

The columns of a string grid are of type TStringColumn (or descendants of it). Note that if you create a custom column type there’s currently no way to add it to the IDE and such columns will need to be created at run time.

Creating StringGrids

StringGrids can be created at run time in the standard way for FireMonkey controls by creating the component and assigning the Parent property,

StringGrid1 := TStringGrid.Create(Self);
StringGrid1.Parent := Self

Columns and Rows

Columns can be added at design time by double-clicking on the grid to open the items editor.

At run time, the ColumnCount property is read only and columns are added by creating them and assigning the Parent property. Columns are deleted by freeing them or, for XE4 and onwards, by calling DisposeOf. The Columns property comes in useful here,

procedure TForm1.Button1Click(SenderTObject);
begin
  StringGrid1
.AddObject(TStringColumn.Create(Self));

  
StringGrid1.Columns[0].DisposeOf;
end

See TFMXObject for more methods for managing the parent/child relationship of controls including inserting new columns and re-ordering columns.

The number of rows can be read and modified with the RowCount property.

Cells

The data in a string grid is stored in the Cells property.

StringGrid1.Cells[0,1] := 'Hello World';
ShowMessage(StringGrid1.Cells[0,2]); 

 
If you try and read a cell in a non-existent column you will get ‘(empty)’ returned rather than an exception and writes to a non-existent column will simply be ignored.

Other Members

Other members for things such as navigation, appearance and headers are the same as TGrid.

See Also

TGrid
TColumn

Version: XE4

Categories:

div title=