zoukankan      html  css  js  c++  java
  • GridView中绑定日期字段格式的定义《收集》

    2008-07-28 08:56

    在GridView的BoundField中定义的日期字段无法像DataGrid的一样定义格式。

    <asp:BoundField DataField="SentDate" DataFormatString="{0:MM/dd/yyyy}" />
    上面的代码不能将日期格式化为预期的效果, 必须将boundfield的HtmlEncode属性设置为false 
    <asp:BoundField DataField="SentDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>
     
    GridView 中 DataFormatString 的使用
     

    用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

  • 相关阅读:
    eclipse中的项目如何打成war包
    【SVN】Please execute the 'Cleanup' command.
    2021.06.02模拟赛DP2
    2021.05.26模拟赛 DP1
    状压DP
    高斯消元
    矩阵快速幂
    2021.05.10讲题
    Luogu P2152[SDOI 2009]Super GCD
    Tarjan
  • 原文地址:https://www.cnblogs.com/Golf9527/p/1558518.html
Copyright © 2011-2022 走看看