//欲实现点击DBGrid的Title对查询结果排序,想作一个通用程序,不是一事一议,
//例如不能在SQL语句中增加Order by ...,因为SQL可能原来已经包含Order by ...,
//而且点击另一个Title时又要另外排序,目的是想作到象资源管理器那样随心所欲。
procedure TfrmChild.DBGrid1TitleClick(Column: TColumn);
var
SqlStr,myFieldName,TempStr: string;
OrderPos: integer;
SavedParams: TParams;
begin
if not (Column.Field.FieldKind in [fkData,fkLookup]) then exit;
if Column.Field.FieldKind =fkData then
myFieldName := UpperCase(Column.Field.FieldName)
else
myFieldName := UpperCase(Column.Field.KeyFields);
while Pos(myFieldName,';')0 do
myFieldName := copy(myFieldName,1,Pos(myFieldName,';')-1)
+ ',' + copy(myFieldName,Pos(myFieldName,';')+1,100);
with TQuery(TDBGrid(Column.Grid).DataSource.DataSet) do
begin
SqlStr := UpperCase(Sql.Text);
// if pos(myFieldName,SqlStr)=0 then exit;
if ParamCount0 then
begin
SavedParams := TParams.Create;
SavedParams.Assign(Params);
end;
OrderPos := pos('ORDER',SqlStr);
if (OrderPos=0) or
(pos(myFieldName,copy(SqlStr,OrderPos,100))=0) then
TempStr := ' Order By ' + myFieldName + ' Asc'
else if pos('ASC',SqlStr)=0 then
TempStr := ' Order By ' + myFieldName + ' Asc'
else
TempStr := ' Order By ' + myFieldName + ' Desc';
if OrderPos0 then SqlStr := Copy(SqlStr,1,OrderPos-1);
SqlStr := SqlStr + TempStr;
Active := False;
Sql.Clear;
Sql.Text := SqlStr;
if ParamCount0 then
begin
Params.AssignValues(SavedParams);
SavedParams.Free;
end;
Prepare;
Open;
end;
点击DBGrid的Title对查询结果排序
今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐点击DBGrid的Title对查询结果排序,希望大家看完后也有个好心情,快快行动吧!