Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override sort mechanism of TClientDataSet

Is it possible to change the way indexes are used in TClientDataSet to sort records? After reading this question, I thought it would be nice to be able to sort string fields logically in a client dataset. But I have no idea how to override default behavior of client dataset when it comes to indexes. Any ideas?

PS: My CDS is not linked to any provider. I'm looking for a way to modify the sort mechanism of the TClientDataSet (or the parent in which the mechanism is implemented) itself.

like image 459
iMan Biglari Avatar asked Jul 29 '13 10:07

iMan Biglari


People also ask

What is the Order of the data in a clientdataset?

If the ClientDataSet is loaded through a DataSetProvider, this order matches that of the DataSet from which the DataSetProvider obtains its data. For example, if the DataSetProvider points to a SQLDataSet that includes a SQL query with an ORDER BY clause, DEFAULT_ORDER will order the records in the same order as that defined by the ORDER BY clause.

How is a persistent Index created in a clientdataset?

A persistent index is created when a ClientDataSet's IndexName property is set to the name of an IndexDef. If IndexName is set at design-time, or is set prior to opening a ClientDataSet, that index is built immediately after the ClientDataSet is opened.

What is default_order in a dataset?

DEFAULT_ORDER represents the original order that the records where loaded into the ClientDataSet. If the ClientDataSet is loaded through a DataSetProvider, this order matches that of the DataSet from which the DataSetProvider obtains its data.

What is Delphi tclientdataset?

Delphi offers a native solution: The TClientDataSet component -- located on the "Data Access" tab of the component palette -- represents an in-memory database-independent dataset.


2 Answers

You cannot override the sort mechanism of a ClientDataSet - unless you rewrite the according part of Midas.

To achieve the correct sorting (whatever logical means) you can introduce a new field and set its values in a way so that, sorted with the standard mechanism, they will give the required sort order.

like image 106
Uwe Raabe Avatar answered Sep 20 '22 02:09

Uwe Raabe


Read the excellent on-line article Understanding ClientDataSet Indexes by Cary Jensen.

It explains how to use various ways of sorting and indexing using IndexDefs, IndexFieldNames and IndexName.

Edit: reply to your comment.

You cannot override a sorting method in TClientDataSet, but you can add do this:

If you want to do custom sorting on anything else than existing fields, then you have to add a Calculated Field, perform a kind of order calculation in the OnCalcFields event, then add that field to the IndexDefs.

like image 24
Jeroen Wiert Pluimers Avatar answered Sep 18 '22 02:09

Jeroen Wiert Pluimers