The Delphi IDE allows you to add fields to a TDataset (descendant e.g. TTable, TQuery, TAdoTable etc.) by right clicking on the component and selecting “Add Field” or “New Field”. For a particular project I didn’t want to do that because I kept changing the query for which I want to add the fields. But since there was one calculated field I had to add fields to the dataset otherwise the OnCalcFields event wouldn’t be called. So I ended up adding the fields in code.

The Delphi IDE allows you to add fields to a TDataset (descendant e.g. TTable, TQuery, TAdoTable etc.) by right clicking on the component and selecting “Add Field” or “New Field”. For a particular project I didn’t want to do that because I kept changing the query for which I want to add the fields. But since there was one calculated field I had to add fields to the dataset otherwise the OnCalcFields event wouldn’t be called. So I ended up adding the fields in code.

Basically that’s easy, you just create a TField descendant (e.g. TStringField,TAutoIncField, TDateTimeField etc.) set the field name and dataset and that’s it. But there are some pitfalls
http://blog.dummzeuch.de/2014/07/12/adding-fields-to-a-tdataset-in-code/

Comments

  1. Question: Does this work when the dataset is open?

    ReplyDelete
  2. Do you know thai in xe6 you can mix design time and runtime me fields?

    ReplyDelete
  3. Marco Cantù No, I didn't know that, but since most of my work is still done with Delphi 2007, it wouldn't help. Also, as I wrote in the blog post, I am currently still experimenting with the actual query, so I am adding and removing fields whenever the query changes. I prefer doing that in the same environment as the sql string, that is, in the code editor.

    ReplyDelete

Post a Comment