查看网上代码后个人完善得出 可共用于任何DBGridEh
复制粘贴过去即可 ^_^b ~ 更加合理哦~
procedure TFormSort.dbgRespTitleClick(Column: TColumnEh);
var
i: Integer;
sKeyFieldName: string;
dataSet: TClientDataSet;
begin
inherited;
if not Assigned(Column.Field.dataSet) then Exit;
dataSet:= TClientDataSet(Column.Field.dataSet);
sKeyFieldName:= Column.FieldName;
if dataSet.FieldByName(sKeyFieldName).FieldKind = fkLookup then
sKeyFieldName:= dataSet.FieldByName(sKeyFieldName).KeyFields;
case Column.Title.SortMarker of
smNoneEh:
begin
Column.Title.SortMarker:= smUpEh;
dataSet.IndexName:= '';
dataSet.IndexFieldNames:= sKeyFieldName;
end;
smUpEh:
begin
Column.Title.SortMarker:= smDownEh;
i:= TClientDataSet(dataSet).IndexDefs.IndexOf('i' + sKeyFieldName);
if i = -1 then
begin
with dataSet.IndexDefs.AddIndexDef do
begin
Name:= 'i' + sKeyFieldName;
Fields:= sKeyFieldName;
DescFields:= sKeyFieldName;
end;
end;
dataSet.IndexFieldNames:= '';
dataSet.IndexName:= 'i' + sKeyFieldName;
end;
smDownEh:
begin
Column.Title.SortMarker:= smNoneEh;
dataSet.IndexName:= '';
dataSet.IndexFieldNames:= '';
end;
end;
end;