OnGetText (and probably OnSetText also) in Delphi

How can I implement OnGetText event for TDAField? I have some integer-encoded values which can be displayed in a number of different formats depending on user preferences, including some pretty complex ones. The standard TDataset allows easy access for this using OnGetText events. But I cannot find a recommended way of implementing such functionality anywhere in the documentation for TDAField or TDAMemTable. Simple lookups will not do the trick.

Hello,

There is a BoundedField member of TDAField class, that has onGetText/onSetText events as it a TField type. So you could manually assign these events. And you should do this every time during opening the table (in onAfterOpen event for example), cause after closing table BoundedField is destroyed.

Hope this helps

Thank you, it works. For example (if anybody else needs this solution):

// Custom OnGetText handler
procedure TForm2.DoGetText(Sender: TField; var Text: string; DisplayText: Boolean);
var
J: Integer;
begin
J := Sender.AsInteger;
case FDisplayMode of
0: Text := Format(’%d м’, [J]);
1: Text := Format(’%d км %d м’, [J div 1000, J mod 1000]);
2: Text := Format(’%d км %d пк + %d’, [J div 1000 + 1, (J mod 1000) div 100 + 1, J mod 100]);
end;
end;

// Attach the custom handler to the field instance
procedure TForm2.tbl_SectionEncodingsAfterOpen(DataTable: TDADataTable);
begin
tbl_SectionEncodings.FieldByName(‘FirstCoord’).BoundedField.OnGetText := Self.DoGetText;
end;

Out of curiosity, is it possible to implement this with a strongly typed table (client-side)?

Hello,

You could implement business rule AfterOpen for data table, by analogy with http://wiki.remobjects.com/wiki/Business_Rules_in_Depth_(Delphi)#Client_Side_Business_Rules article and “Strongly Typed” sample, and again assign onGetText event handler manually. For example:
procedure TBizCustomersClientRules.AfterOpen(Sender: TDADataTable);
begin
inherited;
DataTable.FieldByName(‘CompanyName’).BoundedField.OnGetText:=Self.onMyGetText;
end;

Best regards

Thank you!

Thanks That helped a lot.