Wednesday, December 9, 2009

Formatting Currency in a GridView

To format data in a gridview boundcolumn or Eval statement using the property of the boundcolumn called DataFormatString or include the format in the Eval statement like this: [I added an extra space between the brackets so this would format correctly]
< %# Server.HtmlEncode( Eval("InvoiceAmount", "{0:$#,#.#0}").ToString() ) % >"

I got this from a great article http://blog.stevex.net/string-formatting-in-csharp/

Using the decimal value 4219.60 as my number from the database for a product.

{0}
4219.60

{0:c}
$4,219.60

{0:c2}
$4,219.60

{0:c4}
$4,219.6000

{0:$#.#}
$4219.6

{0:$#,#.#}
$4,219.6

{0:#,#.##}
$4,219.6

{0:$#,#.#0}
$4,219.60

1 comment:

  1. Very nice article..
    But am using this format {0:$#.#} when the value is 0 it is showing only $ it should show like ($0).
    waiting for response...

    Thanks in advance

    ReplyDelete