ClientDataSet 的动态排序可以通过以下代码实现
1.单一字段的升序和降序
2.复合字段的升序和降序
with cds
do
begin
IndexDefs.Update;
if
IndexDefs.IndexOf(
'
index2
'
)
>=
0
then
DeleteIndex(
'
index2
'
);
AddIndex(
'
index2
'
,
'
SortField1, SortField2
'
, [],
''
);
IndexName :
=
'
index1
'
;
First;
end;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
3.复合字段的复合排序
传入参数 SortField1, SortField2, [], SortField2 即可
1.单一字段的升序和降序
with cds
do
begin
IndexDefs.Update;
if IndexDefs.IndexOf( ' index1 ' ) >= 0 then
DeleteIndex( ' index1 ' );
AddIndex( ' index1 ' , SortFieldName, [], '' );
IndexName : = ' index1 ' ;
First;
end;
with cds do
begin
IndexDefs.Update;
if IndexDefs.IndexOf( ' index1 ' ) >= 0 then
DeleteIndex( ' index1 ' );
AddIndex( ' index1 ' , SortFieldName, [], SortFieldName);
IndexName : = ' index1 ' ;
First;
end;
begin
IndexDefs.Update;
if IndexDefs.IndexOf( ' index1 ' ) >= 0 then
DeleteIndex( ' index1 ' );
AddIndex( ' index1 ' , SortFieldName, [], '' );
IndexName : = ' index1 ' ;
First;
end;
with cds do
begin
IndexDefs.Update;
if IndexDefs.IndexOf( ' index1 ' ) >= 0 then
DeleteIndex( ' index1 ' );
AddIndex( ' index1 ' , SortFieldName, [], SortFieldName);
IndexName : = ' index1 ' ;
First;
end;
2.复合字段的升序和降序
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
with cds
do
begin
IndexDefs.Update;
if IndexDefs.IndexOf( ' index2 ' ) >= 0 then
DeleteIndex( ' index2 ' );
AddIndex( ' index2 ' , ' SortField1, SortField2 ' , [], ' SortField1, SortField2 ' );
IndexName : = ' index2 ' ;
First;
end;
begin
IndexDefs.Update;
if IndexDefs.IndexOf( ' index2 ' ) >= 0 then
DeleteIndex( ' index2 ' );
AddIndex( ' index2 ' , ' SortField1, SortField2 ' , [], ' SortField1, SortField2 ' );
IndexName : = ' index2 ' ;
First;
end;
3.复合字段的复合排序
传入参数 SortField1, SortField2, [], SortField2 即可