Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Just wondering how to format an unbound value in a Gridview to currency?
The numbers I want to format are derived from client side script.
Posted
Updated 29-Jan-12 15:00pm
v2
Comments
daniele.arrighi 29-Jan-12 20:04pm    
What do you mean with "unbound value"?
Sergey Alexandrovich Kryukov 29-Jan-12 20:17pm    
We know what is it, don't worry :-)
The question is correct and appropriate.
--SA

Please see the part of MSDN documentation of numeric format string: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^].

Locate the section "The Currency ("C") Format Specifier" and search by the keyword "currency" through all of this text for code samples, specification and related information.

Also, pay attention for dependency on the culture, which can be defined by a current thread culture or specified explicitly. Please see:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^],
http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx[^].

In the second of the articles mentioned above, pay attention for using of CultureInfo which implements the interface IFormatProvider; for the usage, see the code samples.

[EDIT]

As with currency formatting always depends on particular cultures, see also:
http://msdn.microsoft.com/en-us/library/ie/c6zyy3s9.aspx[^].

For thread culture, see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture.aspx[^].

—SA
 
Share this answer
 
v3
Comments
thatraja 29-Jan-12 21:15pm    
Right, 5!
Sergey Alexandrovich Kryukov 29-Jan-12 22:02pm    
Thank you, Raja.
--SA
Wonde Tadesse 29-Jan-12 22:18pm    
5+
Sergey Alexandrovich Kryukov 29-Jan-12 22:41pm    
Thank you, Wonde.
--SA
I'm not sure what did you mean by "unbound value in Gridview", but one option to format fields in UI side will be to use DataFormatString property of BoundField[^]. For example
HTML
<asp:BoundField HeaderText="Price/Unit"
                DataField="UnitPrice" SortExpression="UnitPrice"
                DataFormatString="{0:c}">

will format UnitPrice field of the GridView to Currency. For lists of different DataFormatString ,have a look at this[^] MSDN article.
 
Share this answer
 
Comments
thatraja 29-Jan-12 21:15pm    
Right, 5!
Wonde Tadesse 29-Jan-12 21:29pm    
Thanks
Sergey Alexandrovich Kryukov 29-Jan-12 22:03pm    
Sure, a 5. And I actually explained where to get format strings in my answer.
--SA
Wonde Tadesse 29-Jan-12 22:18pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900