1

When adding persistent fields of a dataset using the Fields Editor, the field names are concatenated to the dataset as TableMyField, I can then access the persistent field's DisplayFormat property in my code as:

TableMyField.DisplayFormat

However, if I don't use the Fields Editor and don't use persistent fields, how can I access the DisplayFormat property at run time?

John Easley
  • 1,551
  • 1
  • 13
  • 23
Aorangi
  • 31
  • 7
  • Dataset's `TField` inherits `DisplayFormat` from its definition, so set it up right for the field in your case. – Victoria Nov 30 '17 at 17:49
  • 1
    @Victoria not sure if this is true. TField decedents implement displayformat for each decending classe's purpose. OP would need to cast, example: `TFloatFIeld(dataset.fieldbyname('Cost')).displayformat := '#,###.00'` etc. – John Easley Nov 30 '17 at 18:10
  • @John, sorry, I missed the word _descendants_. But they inherit that from definition, or am I wrong (no Delphi by hand)? I haven't checked that, but it's the only logical source I can think of. And what would be wrong on the approach that you've shown? – Victoria Nov 30 '17 at 18:41

1 Answers1

1

Since the DisplayFormat property is contained in descending classes of TField, you'd need to cast at runtime. You can do this a couple of different ways.

TNumericField(Dataset.Fieldbyname('CostPrice')).DisplayFormat := '#,###.00';

(Dataset.fieldbyname('CostPrice') as TNumericField).DisplayFormat := '#,###.00';

TNumericField(Dataset.fields[0]).DisplayFormat := '#,###.00';
John Easley
  • 1,551
  • 1
  • 13
  • 23