//描述: cxGrid的 TableView 数据排序与对应的数据集同步, 该方法主要用于打印时
// 的排序与所见到的排序保持一致;
//参数: @tv: 排序的cxGridTableView
//说明: @tv: 对应的数据集只支持 ADOQuery与 ClientDataSet;
procedure cxGridSortSyncToDataSet(tv: TcxGridDBTableView); overload;
//描述: 功能同上, 实现代码一样, 如果有更改就同步更改
procedure cxGridSortSyncToDataSet(tv: TcxGridDBBandedTableView); overload;
procedure cxGridSortSyncToDataSet(tv: TcxGridDBTableView);
const
SortArray: array[soAscending..soDescending] of string = ('ASC', 'DESC');
var
AscFields, DescFields, S, SortOrder: string;
IndexPrint: string;
I: integer;
Index: integer;
cds: TClientDataSet;
begin
S := '';
AscFields := '';
DescFields := '';
if tv.SortedItemCount = 0 then
Exit;
if tv.DataController.DataSource.DataSet is TADOQuery then begin
for I := 0 to tv.SortedItemCount - 1 do begin
SortOrder := SortArray[tv.SortedItems[I].SortOrder];
if S <> '' then
S := S + ', ';
Index := tv.SortedItems[I].Index;
S := S + tv.Columns[Index].DataBinding.Field.FieldName + ' ' + SortOrder;
end;
(tv.DataController.DataSource.DataSet as TADOQuery).Sort := S;
end else if (tv.DataController.DataSource.DataSet is TClientDataSet) then begin
Cds := tv.DataController.DataSource.DataSet as TClientDataSet;
for I := 0 to tv.SortedItemCount - 1 do begin
Index := tv.SortedItems[I].Index;
S := tv.Columns[Index].DataBinding.Field.FieldName +';';
AscFields := AscFields + S;
if tv.SortedItems[I].SortOrder = soDescending then
DescFields := DescFields + S;
end;
if AscFields <> '' then
Delete(AscFields, Length(AscFields), 1); //删除 ;
if DescFields <> '' then
Delete(DescFields, Length(DescFields), 1);
IndexPrint := TimeToStr(Now());
Cds.IndexDefs.Clear;
IndexPrint := TimeToStr(Now());
cds.AddIndex(IndexPrint, AscFields, [], DescFields);
cds.IndexName := IndexPrint;
end;
end;