Using BoundField.DataFormatString to format currency data, no $'s and negative numbers should be in ( )'s

kaelle picture kaelle · Mar 29, 2011 · Viewed 16k times · Source

I have a gridview that I am populating with data for the folks in accounting and they want me to format currency values so that they display without $'s, with commas separating digits and with negative numbers surrounded by ( )

e.g.:

 12345.67 = 12,345.67
-12345.67 = (12,345.67)

I have found lots of examples around the interwebs that get me close but there is either no ( ) around negatives or there is a $ included.

Answer

kaelle picture kaelle · Mar 29, 2011

So I guess basically the question was, what is the String.Format() call that I would make to format a currency value to the aforementioned requirements.

After messing around with some custom formats I figured it out!

var amt = new BoundField ();
amt.DataFormatString = "{0:#,##0.00;(#,##0.00);0}";

Works like a charm.