在DBGrid的TitleClick事件中写代码.var i : integer; sCondition : string; begin if not self.aqMaster.Active then exit; i := Column.Index; if trim(Dbgrid1.Columns[i].FieldName) = 'memo' then exit; if bSort then begin sCondition := trim(DBGrid1.Columns[i].FieldName) + ' DESC'; bSort := false; end else begin sCondition := trim(DBGrid1.Columns[i].FieldName) + ' ASC'; bSort := true; end; aqMaster.Sort := sCondition; aqMaster.First;
如果后台排序,可以在sql于今中增加order by
...
private
{ Private declarations }
procedure setindex;
...
procedure TPMMtInfo.setindex;
begin
//设置排序字段
table1.indexfieldnames:='newitemcode';
table1.IndexFieldNames:='itemcode';
table2.indexfieldnames:='newmtcode';
end;
i : integer;
sCondition : string;
begin
if not self.aqMaster.Active then exit; i := Column.Index;
if trim(Dbgrid1.Columns[i].FieldName) = 'memo' then exit;
if bSort then
begin
sCondition := trim(DBGrid1.Columns[i].FieldName) + ' DESC';
bSort := false;
end
else
begin
sCondition := trim(DBGrid1.Columns[i].FieldName) + ' ASC';
bSort := true;
end; aqMaster.Sort := sCondition;
aqMaster.First;