是啊,7年都没搞定,太惨了,呵呵。<br><br>function DefineIndex(CDS:TClientDataSet; OldFields:String; var IdxFields, IdxDescFields: String): Boolean;<br>//根据传递的OldFields内容(从表格得来的)生成表的索引,格式多个字段间用逗号分隔,如果要递减顺序,字段名后加 Desc <br>(即OldField实际上就是一个Order by 的字句,格式为 Field1,Field2 desc,Field3,....)<br>var tpField,tpDesc:String;<br> i:Integer;<br>begin<br> Result:=False;<br> IdxFields:='';<br> IdxDescFields:='';<br> if StrIsEmpty(OldFields) then //StrIsEmpty 自写的判断是否为空的语句,自已重写吧<br> Exit;<br> For i:=1 to CountStrTimes(',', OldFields) do begin //CountStrTimes 自写的 统计有几个字段的函数,自已想办法重写吧<br> tpField:= Trim(GetAppointStr(',',OldFields, i));<br> tpDesc:='';<br> if StrIsEnd('desc',tpField) then<br> begin<br> tpDesc:=Trim(Copy(tpField,1,Length(tpField)-5));<br> tpField:=tpDesc;<br> end;<br> if Assigned(CDS.FindField(tpField)) then begin<br> if Not StrIsEmpty(tpField) then<br> IDXFields:=IDXFields + Trim(tpField)+ ';';<br> if Not StrIsEmpty(tpDesc) then<br> IDXDescFields:=IDXDescFields + Trim(tpDesc) + ';';<br> end;<br> end;<br>//循环的作用就是依次求出Field1,Field2 desc,Field3,然后判断这个字段是否存在,再判断后面有没关键字desc,分别加到IdxFields与IdxDescFields字段序列中。<br> if Not StrIsEmpty(IDXFields) then<br> Delete(IDXFields,Length(IDXFields),1);<br> if Not StrIsEmpty(IDXDescFields) then<br> Delete(IDXDescFields,Length(IDXDescFields),1);<br><br> Result:=Not StrIsEmpty(IDXFields);<br>end;<br><br>然后呢<br> with ClientDataSetl do begin<br> IndexDefs.Clear;<br> AddIndex('SortReport',IDXFields,[],IDXDescFields);<br> IndexDefs.Update;<br> IndexName:='SortReport';<br> end;<br>就可以了。<br><br>Delphi syntax:<br><br>procedure AddIndex(const Name, Fields: string; Options: TIndexOptions; const DescFields: string = ''; const CaseInsFields: string = ''; const GroupingLevel: Integer = 0 );<br><br>C++ syntax:<br><br>void __fastcall AddIndex(const AnsiString Name, const AnsiString Fields, Db::TIndexOptions Options, const AnsiString DescFields = "", const AnsiString CaseInsFields = "", const int GroupingLevel = 0);<br><br>Description<br><br>Call AddIndex to create a new index for the client dataset.<br><br>Name is the name of the new index.<br><br>Fields is a semicolon-delimited list of the fields to include in the index.Options is a (potentially empty) set that can include up to two TIndexOptions values. The valid members for that set include ixDescending (specifying that the index sorts in descending alphanumeric order) and ixCaseInsensitive (specifying that the index should ignore case when sorting).<br><br>看一下DELPHI对Addinex帮助的说明,也不用摸索7年了。