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

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

문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top