tags:

views:

225

answers:

2

Hello,

as far is I know sorting in a ClientDataSet works over the indexDefs. I can add an indexDef, set the field(s) I want to sort, and over the ixDescending property I can define the direction to sort.

I have a ClientDataSet connected to a dataSource which is the source for a DBGrid. When I now insert a new record in the ClientDataSet, it's either inserted at the top of the table (ixDescending = false) or at the bottom of the table (ixDescending = true).

But I want a descending order AND new records should be at the top at the table - not at the bottom. I tried to change the the indexDefs at runtime to achieve this - but without success. Somebody has an idea how to do this?

A: 

When you have inserted a record unless you set values for the indexed fields they are NULL, and sorted accordingly.

ldsandon
I dont understand - when I don't set a value for the indexed fields the order is ascending. I need an descending order already before a new record is inserted.
ben
The issue could be how a ClientDataset handle NULL values in indexes. They can be considered greater or lesser than any other value. If a CDS uses the latter convention, and you use a descending order, a record with NULL values in the indexed fields will be put at the bottom.
ldsandon
You are right of course. I've chosen another index field now which is populated before posting the new record, and now its there where it should be - Thank you ;)
ben
A: 

I don't know of an elegant solution (although I'm a novice in this area.)

Perhaps there's some way for you to create a temporary boolean field in your ClientDataSet... (Maybe a calculated field?) Suppose you name the new field "NewRecord" and include it in your IndexDef so that it is the most significant sort criteria.

In an .OnCreate event, you set it true (which would be 1 internally?). In an .OnPost event you set it false.

If you don't want to depend on the internal representation of booleans, then you could create a string field and put "ZZZZZZZ" in it. Or an integer field and put MaxInt in it.

Robert Frank