Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the order of fields in an already opened TADODataSet?

Tags:

delphi

ado

I have a TADODataSet that loads some columns from the DB (a SP, but that does not matter). e.g:

SELECT A, B, C, D FROM Foo

Now, after I open the dataset, Is it possible to change the order of fields (or swap them) in that TADODataSet so that it will contain e.g:

C, D, A, B

I want to do it without changing the command text itself. Is it possible?

like image 495
zig Avatar asked Jun 05 '17 13:06

zig


1 Answers

You can use the Index property of the TField class to set the desired order.

Example:

MyDataset.FieldByName('C').Index := 0;
MyDataset.FieldByName('D').Index := 1;
MyDataset.FieldByName('A').Index := 2;
MyDataset.FieldByName('B').Index := 3;

This also works if the fields are persistent:

MyDatasetC.Index := 0;
MyDatasetD.Index := 1;
MyDatasetA.Index := 2;
MyDatasetB.Index := 3;
like image 94
John Easley Avatar answered Oct 09 '22 08:10

John Easley