ClientDataSet建立索引和排序
可用ClientDataSet.AddIndex或ClientDataSet1.IndexDefs.AddIndexDef建立索引。
AddIndex(
const Name:string; ? ? ? ? ? ? ? ? ? //索引名
const Fields:string; ? ? ? ? ? ? ? ? ? ?//索引字段,多個用;隔開
options:TIndexOptions; ? ? ? ? ? ? ?//選項[IxPrimary,IxUnique,ixDescending,ixCaseInsensitive]
const ?DescFields:string; ? ? ? ? ? ? //降序排列字段
const CaseInFields:string ? ? ? ? ? ?//不區分大小寫字段
const GroupingLevel:integer ? ? ? ?//分組級別
);
AddIndexDef用法跟上面類似,在with...do中設置相應屬性
with?ClientDataSet1.IndexDefs.AddIndexDef? do
begin? ??
name:string;
CaseInsFields: string ;
DescFields: string;?
Expression: string ;
Fields: string;?
Options: TIndexOptions;
GroupingLevel: Integer;
end;
下例中使用AddIndex方法,如圖:
***************************************************************************************************************************
具體操作:
***************************************************************************************************************************
DBGrid1.DataSource->DataSource1.DataSet->ClientDataSet1.ProviderName->DataSetProvider1.DataSet->
SQLDataset1.SQLConnection->SQLConnection1具體數據庫(這里連接的是oracle中的EMP表);
SQLDataSet1.CommandText:=SELECT * FROM EMP;ClientDataSet1.Active:=true;
***************************************************************************************************************************
主要代碼:
***************************************************************************************************************************
procedure TForm3.Button1Click(Sender: TObject); beginif ClientDataSet1.IndexName='Index1' thenbeginButton1.Caption:='ENAME升序EMPNO降序';ClientDataSet1.IndexName:='Index2';end elsebeginClientDataSet1.IndexName:='Index1';Button1.Caption:='EMPNO升序ENAME降序';end; end;procedure TForm3.DBGrid1TitleClick(Column: TColumn); begin if not Column.Field.IsBlob then // 不能給大二進制字段建立索引或排序ClientDataSet1.IndexFieldNames := Column.FieldName; end;procedure TForm3.FormCreate(Sender: TObject); begin//EMPNO升序排序ENAME降序排列ClientDataSet1.AddIndex('Index1','EMPNO;ENAME',[],'ENAME');//也可寫成// ClientDataSet2.AddIndex('Index1','EMPNO,ENAME',[ixDescending],'ENAME');//ixDescending被忽略// ENAME升序,EMPNO降序ClientDataSet1.AddIndex('Index2','EMPNO;ENAME',[],'EMPNO');ClientDataSet1.IndexName:='Index1'; end;注意:如果只想對當想緩沖區中的數據排序要用TempClientDataSet.CloneCursor方法克隆到TempClientDataSet或直接用TempClientDataSet.Data:=ClientDataSet1.Data,把ClientDataSet1緩沖區中的數據復制到TempClientDataSet中,ClientDataSet2中的數據就與數據源分離了,再對TempClientDataSet排序。如果不分離數據源就排序,ClientDataSet1會把表中的所有數據讀到本地再排序。
轉載于:https://www.cnblogs.com/sun998/p/6511417.html
總結
以上是生活随笔為你收集整理的ClientDataSet建立索引和排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时光匆匆容颜易老的说说164个
- 下一篇: 前端开发学习的基础网站