How to sort Like use ado method

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jerry0318
Posts: 2
Joined: Mon 14 Aug 2017 17:48

How to sort Like use ado method

Post by jerry0318 » Mon 14 Aug 2017 19:19

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;

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: How to sort Like use ado method

Post by azyk » Tue 15 Aug 2017 09:26

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

jerry0318
Posts: 2
Joined: Mon 14 Aug 2017 17:48

Re: How to sort Like use ado method

Post by jerry0318 » Wed 16 Aug 2017 03:34

solved. Thank you!

Post Reply