单击dbgrid的title进行排序

我找到了使用 ADOQUERY 控件实现的方法,可做参考:
巧用DBGrid控件的Sort属性实现"点击标题栏自动排序功能
很多朋友在使用DBGrid控件时,抱怨没有点击标题栏便可自动排序的功能,实际上通过ADOQuery或其它数据集组件的Sort属性配合 DBGrid的OnTitleClick事件,可以方便地实现此功能。因为Sort属性可以让活动数据集按指定的字段进行排序,也可以选择排序方式(升序 /降序),所以我们只要在OnTitleClick事件中设置排序字段和排序方式即可,如以下代码:
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var i : integer;
begin
for i:= 1 to DBGrid1.Columns.Count do
begin
//恢复所有标题字体为默认
DBGrid1.Columns[i-1].Title.Font.Color := clWindowText;
DBGrid1.Columns[i-1].Title.Font.Style := [];
end;
if ADOQuery1.Sort<>(Column.FieldName+' ASC') then //判断原排序方式
begin
ADOQuery1.Sort := Column.FieldName+' ASC';
Column.Title.Font.Color := clRed; //改变标题行字体为红色,表示当前的排序方式为升序
Column.Title.Font.Style := [fsBold];
end
else begin
ADOQuery1.Sort := Column.FieldName+' DESC';
Column.Title.Font.Color := clBlue; //改变标题行字体为红色,表示当前的排序方式为降序
Column.Title.Font.Style := [fsBold];
end;
end;

 


//欲实现点击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 ParamCount>0 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 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;
    Prepare;
    Open;
  end;

 

又一个方法双击标题排序:


procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
 temp, title: string;
begin
 temp := Column.FieldName;
 qusp.Close;
 if Column.Index <> lastcolumn then
 begin
   if (Pos('↑', DBGrid1.Columns[LastColumn].Title.Caption) > 0) or (Pos('↓', DBGrid1.Columns[LastColumn].Title.Caption) > 0) then
     DBGrid1.Columns[LastColumn].Title.Caption := Copy(DBGrid1.Columns[LastColumn].Title.Caption, 3, Length(DBGrid1.Columns[LastColumn].Title.Caption) - 2);
   qusp.Sql[icount] := 'order by ' + temp + ' asc';
   DBGrid1.Columns[Column.Index].Title.Caption := '↑' + DBGrid1.Columns[Column.Index].Title.Caption;
   lastcolumn := column.Index;
 end
 else
 begin
   LastColumn := Column.Index;
   title := DBGrid1.Columns[LastColumn].Title.Caption;
   if Pos('↑', title) > 0 then
   begin
     qusp.Sql[icount] := 'order by ' + temp + ' desc';
     Delete(title, 1, 2);
     DBGrid1.Columns[LastColumn].Title.Caption := '↓' + title;
   end
   else if Pos('↓', title) > 0 then
   begin
     qusp.Sql[icount] := 'order by ' + temp + ' asc';
     Delete(title, 1, 2);
     DBGrid1.Columns[LastColumn].Title.Caption := '↑' + title;
   end
   else
   begin
     qusp.Sql[icount] := 'order by ' + temp + ' asc';
     DBGrid1.Columns[LastColumn].Title.Caption := '↑' + title;
   end;
 end;
 qusp.Open;
end;

你可能感兴趣的:(sql,活动)