声明:以下内容摘录自http://www.cnblogs.com/david-weihw/archive/2007/09/21/901819.html
注意以下几点
1. 在GridView中的asp:BoundField使用DataFormatString必须设置属性HtmlEncode="False",否则不起作用。
2. 如果需要使用日期类型的格式化字符串,数据实体中对应的字段也应该用日期类型的。
3. 格式化字符串C代表货币单位,需要绑定的数据类型应该是数字类型的。如果是字符串类型的不起作用,需要手动添加格式化字符串为DataFormatString="¥{0:C}"。
GridView中使用DataFromatString与在DataGrid中使用起来有些不同的!在GridView中的BoundField新增了HtmlEncode 属性,且默认是true,这就使得DataFromatString失效!
{0:d} YY-MM-DD
{0:p} 百分比00.00%
{0:N2} 12.68
{0:N0} 13
{0:c2} $12.68
{0:d} 3/23/2003
{0:T} 12:00:00 AM
{0:男;;女}
"{0:C}" 12345.6789 -> $12,345.68
"{0:C}" -12345.6789 -> ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 -> 00012345
"{0:E}" 12345.6789 -> 1234568E+004
"{0:E10}" 12345.6789 -> 1.2345678900E+004
"{0:F}" 12345.6789 -> 12345.68
"{0:F0}" 12345.6789 -> 12346
"{0:G}" 12345.6789 -> 12345.6789
"{0:G7}" 123456789 -> 1.234568E8
"{0:N}" 12345.6789 -> 12,345.68
"{0:N4}" 123456789 -> 123,456,789.0000
"Total: {0:C}" 12345.6789 -> Total: $12345.68
string.format格式结果
String.Format
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
(d) Short date: . . . . . . .
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . .
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
(G) General:. . . . . . . . . Green
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003