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

StackOverflow https://stackoverflow.com/questions/5477451

Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

If it was just numeric without ( ) it would be as simply as {0:N} but since you need ( ) for negative numbers BoundField is not your choice unless you want to do something at sql level or code-behind directly manipulating the datasouce before it is bound to the field.

You next option is to use TemplateField with a Label and set the set accordingly in GV RowDataBound Event in code-behind.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top