得自己在onColumnClick里加上SQL语句进行升序和降序的排序.
procedure TMainForm.dxDBGrid1ColumnClick(Sender: TObject;
Column: TdxDBTreeListColumn);
function GetFieldName(AColumn : TdxDBTreeListColumn) : String;
begin
with TdxDBGrid(Sender).DataSource.DataSet.FieldByName(AColumn.FieldName) do
begin
Result := FieldName;
if FieldKind = fkLookup then Result := KeyFields;
end;
end;
var
ID, i: Integer;
SQLOrderSt: String;
OldSorted: TdxTreeListColumnSort;
begin
with TdxDBGrid(Sender) do
begin
OldSorted := Column.Sorted;
if (GetAsyncKeyState(VK_SHIFT) = 0) then
TdxDBGrid(Sender).ClearColumnsSorted;
if OldSorted = csUp then
Column.Sorted := csDown
else
Column.Sorted := csUp;
with TQuery(DataSource.DataSet) do
begin
ID := FieldByName('ID').AsInteger;
DisableControls;
try
Close;
SQLOrderSt := '';
for i := 0 to SortedColumnCount - 1 do
begin
if SQLOrderSt <> '' then
SQLOrderSt := SQLOrderSt + ', ';
SQLOrderSt := SQLOrderSt + GetFieldName(SortedColumns);
if SortedColumns.Sorted = csDown then
SQLOrderSt := SQLOrderSt + ' DESC';
end;
if SQLOrderSt <> '' then
SQLOrderSt := 'ORDER BY ' + SQLOrderSt;
SQL.Strings[SQL.Count - 1] := SQLOrderSt;
Open;
Locate('ID', ID, []);
finally
EnableControls;
end;
end;
end;
end;