Uses OnSortChange
to apply the order a title click asks for. The grid shows the sort
direction in the title, but never reorders the data itself: the dataset
does, as it knows how.
A click on a title orders the products by that column, ascending, then descending on the next click.
Besides EgFireGrid: System.StrUtils
(IfThen, in the second snippet) and the unit of the dataset
component (here FireDAC.Comp.Client, for
Query1).
For a FireDAC dataset, IndexFieldNames orders the
records in memory, with no new query:
procedure TForm1.Grid1SortChange(Sender: TObject; Column: TEgFireGridColumn; Direction: TEgFireGridSortDirection);
begin
case Direction of
fgsAscending:
Query1.IndexFieldNames := Column.FieldName;
fgsDescending:
Query1.IndexFieldNames := Column.FieldName + ':D';
else
Query1.IndexFieldNames := '';
end;
end;
For a large table, the order can instead be given to the server, with a new query:
Query1.Close;
Query1.SQL.Text := 'SELECT * FROM Products ORDER BY ' + Column.FieldName +
IfThen(Direction = fgsDescending, ' DESC', '');
Query1.Open;
IndexFieldNames names
one field.IndexFieldNames cannot order by a lookup or calculated
field, and ordering by a memo or picture makes little sense. Set those
columns’ Sortable
to fscNone: their title click then fires OnTitleClick
only.fscNone too.Columns
is empty); the grid keeps the sort glyph on the column of the same
field. With the second form, build the ORDER BY from a list
of allowed field names rather than from any text, as a field name is put
into SQL.