Discussion:
Grid - Zahlen dynamisch formatieren
(too old to reply)
Nicolas Bronke
2013-05-25 14:23:40 UTC
Permalink
Eigentlich dachte ich das formatieren in einem TDBGRID ist nicht das
Problem, aber bei dynamischen Spalten scheint das nicht zu klappen:

field := sqlInfo.FindField(sSpaltenName);
if field<>nil then begin
Field.DisplayLabel := sDisplaylabel;
Field.DisplayWidth := nWidth;
Field.EditMask := sFormat;
//


Field ist TField. Ich übergebe in Sformat z.B. #,##0.00 um Zahlen
entsprechend zu formtieren. Aber das klappt leider nicht. Im Grid
bleiben die Zahlen unformatiert.

Halt jemand eine Idee?

Es geht um Dedlphi 5

Grüße
Nicolas
Alan Lloyd
2013-06-01 07:18:10 UTC
Permalink
Nicholas

Ich habe nur ein bischen deutsch <g>

I think I understand your problem, but (as I see it in Delphi 5 Help)
TField.DisplayLabel is a mask for input controlling what a user can input,
not for output. And basically you're operating with a TField (from your
Find()) which does not have a user-definable text formatter.

You get a TField returned by FindField() which does not have a method to
format the data. But your field is most likely actually a TFloatField which
is descended from TField via TNumericField. TNumericField _does_ have a
published DisplayFormat property (which descends to TFloatField) and so it
is accessible from _your_ sSpaltenName field by typecasting.

To get access to the TFloatField properties you must typecast Field to a
TFloatField. But to protect your program against the faint possibility that
Field is not a TFloatField you should code :

if (Field is TFloatField) then
TFloatField(Field).DisplayFormat := '#,##0.00';

Or you can do this check at the top of your code :

if (Field is TFloatField) then begin // nil is not a TFloatField either
TFloatField(Field).DisplayFormat := '#,##0.00';
Field.DisplayLabel := . . . etc

Alternatively you can use the "as" operator in a "try . . . except"
statement and catch the error with appropriate code.

Another alternative is to write an OnGetText handler.

If this is not clear as English perhaps you can get a colleague to
translate.

Alan Lloyd
Post by Nicolas Bronke
Eigentlich dachte ich das formatieren in einem TDBGRID ist nicht das
field := sqlInfo.FindField(sSpaltenName);
if field<>nil then begin
Field.DisplayLabel := sDisplaylabel;
Field.DisplayWidth := nWidth;
Field.EditMask := sFormat;
//
Field ist TField. Ich übergebe in Sformat z.B. #,##0.00 um Zahlen
entsprechend zu formtieren. Aber das klappt leider nicht. Im Grid bleiben
die Zahlen unformatiert.
Halt jemand eine Idee?
Es geht um Dedlphi 5
Grüße
Nicolas
Nicolas Bronke
2013-06-04 07:02:07 UTC
Permalink
Post by Alan Lloyd
To get access to the TFloatField properties you must typecast Field to a
TFloatField. But to protect your program against the faint possibility that
if (Field is TFloatField) then
TFloatField(Field).DisplayFormat := '#,##0.00';
Thank you. I solved meanwhile like you escribed it.
Regards
Nicolas

Loading...