Link to home
Start Free TrialLog in
Avatar of girlswants_me
girlswants_me

asked on

Display currency in DBGrid

How can i display the values of field Price in DBGrid to a Currency? Can anybody give me a code? I do not want to use the onGetText(Sender: TField; var Text: String; DisplayText: Boolean);

By the way, the fieldtype of "Price" is Float.

Anybody?
Avatar of girlswants_me
girlswants_me

ASKER

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Where should i place the
TFloatField(Table1.FieldByName('Price')).DisplayFormat:='$0,000.00'; ???
I got it!!! A million Thanks to you. I'll increase your points to 150 coz i'm very much happy with your genius answer.

ps. my DBGrid doesn't auto adjust the field width whenever the value is 1,000,000.00. It covers a portion of the display, can you teach me how to make my DBGrid to auto adjust the width?

Thank you very much!

You are very welcome....

As to the second question; the float field will have a default display width based on 10 characters (the field's property DisplayWidth will be set to 10). To cover 1 million, formatted out as '$1,000,000.00' you should set the DisplayWidth to 13, or 14 to be on the safe side. This can be done either at design time (right click, field editor, add all fields, select field, update the displaywidth)  or at runtime, eg:

  with TFloatField(Table1.FieldByName('Price')) do
  begin
     DisplayFormat:='$0,000.00';
     DisplayWidth:=14;
  end;

You can also apply this value after the grid has loaded, and it will adjust itself to the newly changed value.

As far as a true autosizing feature goes, this would require a lot more work; eg walking all column values and determing the output text length based on grid font and formatted size, then using the max length obtained and applying this to the grid column's width. If you need help with this, its probably better to open a new question.

Regards,
Russell