GridView 中 DataFormatString 的使用

來源:互聯網
上載者:User

用DataFormatString格式化GridView
在GridView裡面顯示資料,要顯示的資料有好多位小數,就想讓它只顯示兩位小數,在delphi裡,直接用DisplayFormat就行了,在.net中,查了半天msdn,發現使用DataFormatString是可以實現這個功能的,但是怎麼設定就不起作用,最後發現,由於2.0出於安全性的考慮,還要同時設定HtmlEncode = false,才能夠使DataFormatString生效.
留個記號,下次用的時候,就不用浪費N多時間了.
還有還有,DataFormatString = "{0:F}",是預設格式,顯示兩位小數,如果需要顯示的小數位元為其他值,DataFormatString = "{0:Fn}"即可.

<asp:BoundColumn DataField="時間" HeaderText="時間" DataFormatString="{0:yyyy/MM/dd}"></asp:BoundColumn>

 

 

DataFormatString="{0:格式字串}"

在DataFormatString 中的 {0} 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;

數字、貨幣格式:
在指定的格式符號後可以指定小數所要顯示的位元。例如原來的資料為「1.56」,若格式設定為 {0:N1},則輸出為「1.5」。其常用的數值格式如下表所示:

格式字串 輸入 結果
"{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

常用的日期時間格式:

格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細時間格式 HH:mm:ss

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.