关闭

DBGrid单击标题自动排序

741人阅读 评论(0) 收藏 举报
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
 s,cFieldName:string;
 i:integer;
 DataSet:TDataSet;
begin
 Try DataSet:=Column.Grid.DataSource.DataSet;
 if not DataSet.Active then exit;
 if Column.Field.FieldKind=fkLookup then
  cFieldName:=Column.Field.KeyFields
 else
  if Column.Field.FieldKind=fkCalculated then
   cFieldName:=Column.Field.KeyFields
  else
   cFieldName:=Column.FieldName;
 if DataSet is TCustomADODataSet then
 begin
  s:=TCustomADODataSet(DataSet).Sort;
  if s='' then
  begin
   s:=cFieldName;
  end else
  begin
   if Pos(cFieldName,s)<>0 then
   begin
    i:=Pos('DESC',s);
    if i<=0 then
    begin
     s:=s+' DESC';
    end else
    begin
     Delete(s,i,4);
    end;
   end else
   begin
    s:=cFieldName;
   end;
  end;
  TCustomADODataSet(DataSet).Sort:=s;
 end else
  if DataSet is TClientDataSet then
  begin
   if TClientDataSet(DataSet).indexfieldnames<>'' then
   begin
    i:=TClientDataSet(DataSet).IndexDefs.IndexOf('i'+Column.FieldName);
    if i=-1 then
    begin
     with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
     begin
      Name:='i'+Column.FieldName;
      Fields:=Column.FieldName;
      DescFields:=Column.FieldName;
     end;
    end;
    TClientDataSet(DataSet).IndexFieldNames:='';
    TClientDataSet(DataSet).IndexName:='i'+Column.FieldName;
   end else
   begin
    TClientDataSet(DataSet).IndexName:='';
    TClientDataSet(DataSet).IndexFieldNames:=column.fieldname;
   end;
  end;
 Except
 End;
end;
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:392475次
    • 积分:5490
    • 等级:
    • 排名:第4840名
    • 原创:134篇
    • 转载:132篇
    • 译文:1篇
    • 评论:21条
    文章分类
    最新评论