1.添加引用:DBClient
2.置DBGridEh控件屬性:
設置dghAutoSortMarking屬性為true
設置希望排序的Column的TitleButton屬性為true
3.//表格排序過程
procedure OrderGrid(dataSet:TClientDataset;orderField:string);
var
i:integer;
begin
if dataSet.IndexFieldNames<>'' then
begin
i:=TClientDataSet(DataSet).IndexDefs.IndexOf('i'+orderField);
if i=-1 then
begin
with Dataset.IndexDefs.AddIndexDef do
begin
Name:='i'+orderField;
Fields:=orderField;
DescFields:=orderField;
end;
end;
dataSet.IndexFieldNames:='';
dataSet.IndexName:='i'+orderField;
end
else
begin
dataSet.IndexName:='';
dataSet.IndexFieldNames:=orderfield;
end;
end;
4.在DBGridEh的OnTitleClick事件中調用
OrderGrid(DM.ClientDataset,Column.FieldName);