Search code examples
delphitclientdataset

ClientDataSet: Store data in the order they are appended


It seems, when I append data to the TClientDataSet they are not stored in the sequence I have appended them. Sometimes they are and sometimes not. Is there an option to have the data in the same sequence, as I e.g. read them from a file or Database?

I want to avoid creating a separate column to store the position and set an index on them.


Solution

  • With databases and this includes the TClientDataset, you can never assume that the data will come out of the database in the order you put them in, or in any particular order.

    If order is important, use another column to store the insertion order, or deduce the order from another column.

    One of the reasons this happens is that the database, when a row is deleted, doesn't "bunch" up the other rows to fill the gap, but re-uses the gap when a new record is added.

    There is an ftAutoInc field type that can be used with TClientDataset, so there isn't much work to do to look after the new column.