没有任何DBGrid可以永久性的解决排序问题,因为你不知道
DBGird.DataSourec.DataSet是什么,TTable,TQuery,TClientDataSet,或者是
其它的第三方控件,所以你需要解决的不是DBGird的排序,而是DBGird.DataSourec.DataSet
的排序。这里有一段代码你可以参考:
//==============================================================================
//点击DBGrid标题栏对查询结果排序************************************************
//==============================================================================
function DBGridTitleOrder(Column: TColumn): integer;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TTable*****************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TableOrder;
begin
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TQuery*****************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure QueryOrder;
var
SQLStr, OrderFieldName, TempStr: string;
i, OrderPos: integer;
SavedParams: TParams;
begin
if not (TQuery(TDBGrid(Column.Grid).DataSource.DataSet).State in [dsBrowse]) then
Exit;
SavedParams := nil;
for i:=0 to Column.Grid.FieldCount-1 do
begin
{TDBGrid(Column.Grid).Columns.Font.Color := clBlack;}
TDBGrid(Column.Grid).Columns.Title.Caption := TDBGrid(Column.Grid).Columns.Field.DisplayName;
end;
if not (Column.Field.FieldKind in [fkData,fkLookup]) then
Exit;
if Column.Field.FieldKind=fkData
then
OrderFieldName := LowerCase(Column.Field.FieldName)
else
OrderFieldName := LowerCase(Column.Field.KeyFields);
while Pos(OrderFieldName,';')<>0 do
OrderFieldName := copy(OrderFieldName,1,Pos(OrderFieldName,';')-1)+','+copy(OrderFieldName,Pos(OrderFieldName,';')+1,100);
with TQuery(TDBGrid(Column.Grid).DataSource.DataSet) do
begin
SQLStr := LowerCase(SQL.Text);
if ParamCount>0 then
begin
SavedParams := TParams.Create;
SavedParams.Assign(Params);
end;
OrderPos := pos('order',SQLStr);
if (OrderPos=0) or (pos(OrderFieldName,copy(SQLStr,OrderPos,100))=0) then
begin
Result := 1;
TempStr := ' order by ' + OrderFieldName + ' asc';
{Column.Title.Caption := Column.Title.Caption + '(▲)';}
{Column.font.Color := clRed;}
end else
if pos('asc',SQLStr)=0 then
begin
Result := 1;
TempStr := ' order by ' + OrderFieldName + ' asc';
{Column.Title.Caption := Column.Title.Caption + '(▲)';}
{Column.font.Color := clRed;}
end else
begin
Result := 2;
TempStr := ' order by ' + OrderFieldName + ' desc';
{Column.Title.Caption := Column.Title.Caption + '(▼)';}
{Column.font.Color := clGreen;}
end;
if OrderPos<>0 then
SQLStr := Copy(SQLStr,1,OrderPos-1);
SQLStr := SQLStr + TempStr;
Active := False;
SQL.Clear;
SQL.Text := SQLStr;
if ParamCount>0 then
begin
Params.AssignValues(SavedParams);
SavedParams.Free;
end;
Open;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TADODataSet************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ADODataSetOrder;
begin
with (TDBGrid(Column.Grid).DataSource.DataSet as TCustomADODataSet) do
begin
if (Pos(Column.Field.FieldName, Sort)=1)
and (Pos(' DESC', Sort)=0) then
begin
Result := 2;
Sort := Column.Field.FieldName + ' DESC'
end else
begin
Result := 1;
Sort := Column.Field.FieldName + ' ASC';
end;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TClientDataSet*********************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ClientDataSetOrder;
begin
TDBGrid(Column.Grid).Tag := TDBGrid(Column.Grid).Tag xor Round(Exp(Column.Index * Ln(2)));
with (TDBGrid(Column.Grid).DataSource.DataSet as TClientDataSet) do
begin
if Column.Field.DataType<ftAutoInc then
begin
if (TDBGrid(Column.Grid).Tag and Round(Exp(Ln(2) * Column.Index)))=0 then
begin
AddIndex(Column.FieldName + 'InxDES', Column.FieldName, [ixDescending]);
IndexName := Column.FieldName + 'InxDES';
Result := 2;
end else
begin
AddIndex(Column.FieldName + 'InxASC', Column.FieldName, []);
IndexName := Column.FieldName + 'InxASC';
Result := 1;
end;
end;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
begin
if (TDBGrid(Column.Grid).DataSource.DataSet is TTable) then
TableOrder
else
if (TDBGrid(Column.Grid).DataSource.DataSet is TQuery) then
QueryOrder
else
if (TDBGrid(Column.Grid).DataSource.DataSet is TADODataSet) then
ADODataSetOrder
else
if (TDBGrid(Column.Grid).DataSource.DataSet is TClientDataSet) then
ClientDataSetOrder;
end;