Increasing and decreasing ordering in ClientDataSet

9

I have to sort clientdataset for 3 fields at the same time:

1 - Active (0 or 1)
2 - date (dd / mm / yyyy)
3 - name

The problem is that I need to do different sorts for each of them, the 1st descending and the other ascending.

Is there a way to do this in clientdataset or is another approach required?

    
asked by anonymous 17.02.2014 / 14:30

1 answer

12

Yes, it is possible. Just use the indexes offered by the DataSet.

I find it interesting to address a bit about the indexes:

There are basically two types of indices in Delphi: Temporary and persistent .

Temporary indices are created using IndexFieldNames. Let's say you have a ClientDataSet with the fields: FirstName, LastName and DateNation. If I want to sort my records by the last name and then the first one, I could do this:

ClientDataSet1.IndexFieldNames := 'UltimoNome;PrimeiroNome';  

At any time this can be changed.

ClientDataSet1.IndexFieldNames := 'UltimoNome';  
// etc...

When you do this, delphi creates the indexes internally and associates them with the dataset. So they are called temporary .

In your case this will not serve you, I wrote only for enrichment. In your case a persistent index will be more appropriate:

Persistent indexes are created in the IndexDefs of your DataSet. So you can, for each index, define a series of properties. They are used in the same way as temporary indexes, just use the name given to the persistent index when they are created in design-time.

To create a Persistent Index in RunTime, just use the code snippet below:

with ClientDataSet1.IndexDefs.AddIndexDef do 
begin 
  Name := 'OrdenarUltimoPrimeiroNomedx'; 
  Fields := 'UltimoNome;PrimeiroNome';
  Options := [ixDescending, ixCaseInsensitive]; 
end; 
ClientDataSet1.IndexName := 'OrdenarUltimoPrimeiroNomedx'; 

EDIT: I talked so much and I did not give an exact answer.

You can create as many persistent indexes as you need:

with ClientDataSet1.IndexDefs.AddIndexDef do 
begin 
  Name := 'OrdenarUltimoPrimeiroNomedx'; 
  Fields := 'UltimoNome;PrimeiroNome';
  Options := [ixDescending, ixCaseInsensitive]; 
end; 

with ClientDataSet1.IndexDefs.AddIndexDef do 
begin 
  Name := 'DataNascimentoAscdx'; 
  Fields := 'DataNascimento';
  Options := [ixAscending]; 
end; 

ClientDataSet1.IndexName := 'DataNascimentoAscdx;OrdenarUltimoPrimeiroNomedx'; 

In the above example I will sort by the Ascending birth date and then by the Last Name and First Descending Name.

If you want a good reference, I suggest you visit this link :

    
17.02.2014 / 17:09