• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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?
0
girlswants_me
Asked:
girlswants_me
  • 3
  • 2
1 Solution
 
girlswants_meAuthor Commented:
Thanks in advance.
0
 
Russell LibbySoftware Engineer, Advisory Commented:

In the table/query/dataset, make sure the fields are added (right click, field editor, add all fields), then select the field and set the display format property to: $0,000.00

You can also do this programmatically by getting the Field, casting as TNumericField, eg:

  TNumericField(Table1.FieldByName('Price')).DisplayFormat:='$0,000.00';

---

Regards,
Russell
0
 
girlswants_meAuthor Commented:
Where should i place the
TFloatField(Table1.FieldByName('Price')).DisplayFormat:='$0,000.00'; ???
0
 
girlswants_meAuthor Commented:
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!
0
 
Russell LibbySoftware Engineer, Advisory Commented:

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

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now