Page 1 of 1

How to sort Like use ado method

Posted: Mon 14 Aug 2017 19:19
by jerry0318
I wrote two examples, want DBGRID to be able to sort the title after pressing
How to achieve it? You can not re-SQL orders to SQL SERVER

procedure TForm1.SMDBGrid1TitleClick(Column: TColumn);
begin
(Column.Grid as TSMDBGrid).ClearSort;

with ADOQuery1 do
begin
if RightStr(Sort, 3) = 'ESC' THEN
begin
TSMDBColumn(Column).SortType := stAscending;
Sort := Column.FieldName + ' ASC' ;
end
else
begin
TSMDBColumn(Column).SortType := stDescending ;
Sort := Column.FieldName + ' DESC';
end;
end;
end;

procedure TForm1.SMDBGrid2TitleClick(Column: TColumn);
begin
if UniQuery1.IsEmpty then Exit ;

try
Screen.Cursor := crSQLWait ;
(Column.Grid as TSMDBGrid).ClearSort;

with UniQuery1 do
begin
if RightStr(GetOrderBy, 3) = 'ESC' THEN
begin
TSMDBColumn(Column).SortType := stAscending;
SetOrderBy(Column.FieldName + ' ASC');

end
else
begin
TSMDBColumn(Column).SortType := stDescending ;
SetOrderBy(Column.FieldName + ' DESC' );
end;
Close ;
Open ;
end;
finally
Screen.Cursor := crDefault ;
end;
end;

Re: How to sort Like use ado method

Posted: Tue 15 Aug 2017 09:26
by azyk
To control local dataset sorting, you can use the TUniQuery.IndexFieldNames property. For this, in your sample, replace the line

Code: Select all

SetOrderBy(Column.FieldName + 'ASC');
with

Code: Select all

UniQuery1.IndexFieldNames: = Column.FieldName + 'ASC';
And the line

Code: Select all

SetOrderBy(Column.FieldName + 'DESC');
with

Code: Select all

UniQuery1.IndexFieldNames: = Column.FieldName + 'DESC';
You can read more about the IndexFieldNames property in our online documentation: https://www.devart.com/unidac/docs/?dev ... dnames.htm

Re: How to sort Like use ado method

Posted: Wed 16 Aug 2017 03:34
by jerry0318
solved. Thank you!