I’ve been working on an ASP.NET project lately, which means it’s high time I write something down to build up my own knowledge base and track down my trail of learning how to code effectively. Hence this article, just another ASP.NET record.
Data Formatting Expression
Back on track, topic here is on data format in a GridView object. To get the proper data format shown in GridView, we should apply the appropriate data formatting expression to them. The data formatting expression consists of an optional literal part and a format specification part that looks like:
{0:format specifier}
where 0 is the Parameter Index indicating the first (and only) data element to be formatted; format specifier is self-explanatory. Examples are given below (via):
Data Formatting Expression
|
Applied to Types
|
Description
|
Price: {0:C}
|
numeric/decimal
|
“Price:” is shown, followed by the number formatted as currency.
|
{0:D4}
|
integer
|
Shows an integer in a zero-filled 4-char-long string format.
|
{0:N2}%
|
numeric
|
Accuracy is set to 2 digits after decimal point. Shown with a percentage sign.
|
{0:000.0}
|
numeric/decimal
|
Show 1 digit after decimal point. Zeroes are filled in if the number is less than 100.
|
{0:D}
|
date/datetime
|
Standard DateTime format(e.g. Saturday, November 08, 2008”).
|
{0:d}
|
date/datetime
|
Short DateTime string like 11/06/08.
|
{0:yy-MM-dd}
|
date/datetime
|
08-11-08.
|
Property for Data BoundField in GridView – DataFormatString
Usage: DataFormatString = “{0: format specifier}”
aspx code:
<Columns>
<asp:BoundField HeaderText="ETA" DataField="OperationDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
</asp:BoundField>
<asp:BoundField HeaderText="Total" DataField="TotalRate" DataFormatString="{0:C}" HtmlEncode="False">
</asp:BoundField>
</Columns>
</asp:GridView>
Notice: Special attention should be paid to the “HtmlEncode” property. With its default value set to “True”, the DataFormatString will be invalid! Set it to “False” to make DataFormatString work.
发表回复