We help IT Professionals succeed at work.

Formatting a currency in a DBGrid

JDN
JDN asked
on
Hi Experts,

Can anyone tell me if it's possible to format a column in a DBGrid to display currency data. I can't find anything about this in the Delphi help.
The DBGrid is linked to an SQL Server table, the column "Price" is of type SmallMoney.

For instance: I like to see "4.70" instead of "4.7", and when I enter ".5", I like to see "0.50".

Thanks for any help.
JDN
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
set the displayformat for this tfield to #,##0.00
JDN

Author

Commented:
Well, how does this work with displayformat?
I can't use DBGrid.Fields[n].displayformat := '#,##0.00' obviously, because displayformat is'nt a know property for TField.
The Delphi help tells me that displayformat is a propery of TNumericField.
Can you give me some more information on this? Fields[n] in the above line is the SQL Server field (type = SmallMoney) which contains the currency data.

Thanks,
JDN
CERTIFIED EXPERT
Top Expert 2004
Commented:
try

tfloatfield(dbgrid1.datasource.dataset.fieldbyname('TheNameOfTheField')).Displayformat := '#,##0.00';

at appstart
CERTIFIED EXPERT
Top Expert 2004

Commented:
or just

tnumericfield(dbgrid1.datasource.dataset.fieldbyname('TheNameOfTheField')).Displayformat := '#,##0.00';

meikl ;-)
CERTIFIED EXPERT
Top Expert 2004

Commented:
just to ask

do you use ado or bde?
JDN

Author

Commented:
Thank you, this works fine. Points are for you!
CERTIFIED EXPERT
Top Expert 2004

Commented:
thanks,
glad to helped you ;-)