ASP.net DataFormatString

 

The DataGrid's BoundColumn control allows the data it displays to be formatted through the DataFormatString property. The DataFormatString property is a string property, and should be assigned a value of the form: {0:Format}. The formatting that can be applied to the DataSource field value being displayed by the BoundColumn depends upon the type of the field. This FAQ examines how to provide formatting for numeric values. (For information on formatting date and time values, be sure to check out the FAQ Formatting Date and Time Data.)

There are eight predefined numeric formatting specifies, summarized in the table below. For the Example column in the table below, the value used is 2003.0515, or 2003 for format providers that require integers.

Format PatternNameExample
C or cCurrency format$2,003.05
D or dDecimal format
(Works for integers only!)
2,003
E or eScientific (exponential) format2.003052e+003
F or fFixed-point format2003.05
G or gGeneral format2003.0515
N or nNumber format2,003.05
P or pPercent format2,00305.15%
X or xHexadecimal format
(Works with integers only!)
7D3

So, if we have a DataSource field that is, say, a price for our product, we can format the price as a currency using the following DataGrid declaration:

      <asp:DataGrid runat="server" AutoGenerateColumns="False" ...> <Columns> ... <asp:BoundColumn DataField="Price" DataFormatString="{0:c}" ... /> ... </Columns> </asp:DataGrid> 
    

Notice that we can have the data displayed in a currency formatting by simply setting the DataFormatString to {0:c}, or, in general, to {0:Format}.

Formatting Depends Upon the Web Server's Culture Settings
The output for the various formats depends upon the Web server's Culture Settings. Through the Regional Settings in the Control Panel, you can specify how Numbers, Dates, and Currencies should be displayed.

Formatting Non-Numeric Data as Numeric Data
Imagine that for some reason the data you want to format as, say, a currency, is not of a numeric datatype. That is, what if the data is a string, but has as its contents just numbers? The {0:c} format specify won't display the string as a currency, since it only formats numeric data into currency. The solution is to use a TemplateColumn, cast the string data to numeric data, and then format it using the currency formatter.

To accomplish this, use the following DataGrid declaration:

      <asp:DataGrid runat="server" AutoGenerateColumns="False" ...> <Columns> ... <asp:TemplateColumn> <ItemTemplate> <%# String.Format("{0:c}", Convert.ToInt32(Container.DataItem("ColumnName"))) %> </ItemTemplate> </asp:TemplateColumn> ... </Columns> </asp:DataGrid> 
    
VB.NET

 

The above code first uses Convert.ToInt32() to convert the string DataSource field ColumnName into an integer. It then uses the String.Format() method to format the integer as a currency. Note that the above code will throw a runtime exception if the dat in the ColumnName field cannot be cast to an integer. Therefore, only use this method if you are certain that the data you will be getting from the DataSource field is indeed castable to a numeric value.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值