Ascending and decreasing ordination in the Clientdataset

Asked

Viewed 19,752 times

10

I have to order the clientdataset 3 fields at the same time:

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

The problem is that I need to make different ordinations for each of them, the first decreasing and the other increasing.

There’s a way to do that in clientdataset or some other approach will be required?

2 answers

15


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

I find it interesting to talk a little about the indices:

There are basically two types of indices in Delphi: The temporary and the persistent.

Indices temporary are created using Indexfieldnames. Let’s assume you have a Clientdataset with Fields: Primeironome, Ultimonome and Datanascimento. If I wanted to sort my records by last name and then by first, I could do that:

ClientDataSet1.IndexFieldNames := 'UltimoNome;PrimeiroNome';  

At any time this can be changed.

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

When you do this Delphi creates the indices internally and associates them to the dataset. That’s why they’re called temporary.

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

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

To create an Index Persistent 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 ended up not giving 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 birth date Ascendant and then by Last Name and First Descending Name.

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

  • Thank you. I just had to explain how to manage to order some fields in the ascending order and others as decreasing. yet with your explanation I was able to do here. Following your last example, if I want you to order the surname in descending order, but the name in ascending order would look like this: <! -- language: lang-delphi -->&#xA; with ClientDataSet1.IndexDefs.AddIndexDef do &#xA; begin &#xA; Name := 'OrdenarNomedx'; &#xA; Fields := 'UltimoNome;PrimeiroNome';&#xA; descFields := 'UltimoNome'&#xA; Options := [ixDescending]; end; Clientdataset1.Indexname := 'Ordinancex';

  • @Stribus really I could give a attention to this. I edited the answer to make it clearer how to solve your problem. Thanks for the touch.

  • Man, congratulations, it was great, accurate and satisfactory your answer.

  • Options := [ixAscending]; Does not accept this property!

0

Depending on the version of Delphi you have can do so:

ClientDataSet1.IndexFieldNames := 'UltimoNome:D;PrimeiroNome'; 

the statement ':D' indicates that it is in descending order, if leaving without anything the cds understands that it is Ascendant. If your Delphi does not work, then use the persistent index.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.