zoukankan      html  css  js  c++  java
  • DataGrid数据格式设置表达式(转)

    转自http://hi.baidu.com/gantianamin2001/blog/item/ec4de8ec6af0752263d09f18.html

    DataGrid-数据格式设置表达式 - [.Net]

    看下文以前先看我的应用图,如果对你有用,请看下去:

    DataFormatString="{0:N0}%“
    DataFormatString="${0:N2}"
    DataFormatString="{0:N0}个"
    DataFormatString="No.{0:N0}"
    DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"

    ==========================以下是转载的原文=========================================

    Tag:

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://xiekeli.blogbus.com/logs/10004523.html

    数据格式设置表达式

    .NET Framework 格式设置表达式,它在数据显示在列中之前先应用于数据。
    此表达式由可选静态文本和用以下格式表示的格式说明符组成:

    {0:format specifier}

    0 是参数索引,它指示列中要格式化的数据元素;因此,通常用零来指示第一个(且唯一的)元素。
    format specifier 前面有一个冒号 (:),它由一个或多个字母组成,指示如何格式化数据。
    可以使用的格式说明符取决于要格式化的数据类型:日期、数字或其他类型。

    下表显示了不同数据类型的格式设置表达式的示例。有关格式设置表达式的更多信息,请参见格式化类型。

    格式设置表达式
    应用于此数据类型
    说明
    Price: {0:C}
    numeric/decimal
    显示“Price:”,后跟以货币格式表示的数字。货币格式取决于通过 Page 指令或 Web.config 文件中的区域性属性指定的区域性设置。
    {0:D4}
    integer(不能和小数一起使用。)
    在由零填充的四个字符宽的字段中显示整数。
    {0:N2}%
    numeric
    显示精确到小数点后两位的数字,后跟“%”
    {0:000.0}
    numeric/decimal
    四舍五入到小数点后一位的数字。不到三位的数字用零填充。
    {0:D}
    date/datetime
    长日期格式(“Thursday, August 06, 1996”)。日期格式取决于页或 Web.config 文件的区域性设置。
    {0:d}
    date/datetime
    短日期格式(“12/31/99”)。
    {0:yy-MM-dd}
    date/datetime
    用数字的年-月-日表示的日期(96-08-06)。

    ASP.NET设置数据格式应用示例:
           {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:男;;女}

    DataGrid数据格式的Format-- DataFormatString

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

    如原来的数据为「12.34」,若格式设定为 {0:N1},则输出为「12.3」

    格式字符串 资料 结果
    "{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

    DateTime.Now.ToString("yyyy-MM-dd");//这种模式最好
    aspx:
    <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="204px">
    <Columns>
    <asp:BoundColumn DataField="date" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>

    </asp:datagrid>

        在我们从业务逻辑层获得数据实体时候,接下来的事情就是要绑定到控件中。数据实体中的一些字段可以直接绑定到界面中,但是有一些字段需要重新格式化格式。比如货币单位字段,需要显示货币符号和每隔三位显示分隔符;再比如日期字段,数据库中存放的是日期和时间,但是在界面上需要按照XXXX年XX月XX日的格式显示。这时候我们就用到了DataFormatString属性。

    <asp:GridView ID="grvResult" runat="server" AutoGenerateColumns="False" Width="100%">

        <Columns>

            <asp:BoundField HeaderText="预定日期" DataField="OperationDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">

            </asp:BoundField>     

            <asp:BoundField HeaderText="订单总计" DataField="TotalRate" DataFormatString="{0:C}" HtmlEncode="False">

            </asp:BoundField>

        </Columns>

    </asp:GridView>

    例如上面的代码展示了日期和货币两种绑定方式。DataFormatString中的{0}是固定的格式,这和String.Fromat(“{0}”, someString)中的{0}是一个用法,表示绑定上下文的参数索引编号。然后,在后面加入格式化字符串,具体的使用方法可以参考MSDN。

    这里需要注意以下几点
    1. 在GridView中的asp:BoundField使用DataFormatString必须设置属性HtmlEncode="False",否则不起作用。
    2. 如果需要使用日期类型的格式化字符串,必须数据实体中对应的字段也应该日起类型的。
    3. 格式化字符串C代表货币单位,需要绑定的数据类型应该是数字类型的。如果是字符串类型的不起作用,需要手动添加格式化字符串为DataFormatString="{0:C}"

    总结:
         GridView中使用DataFromatString与在DataGrid中使用起来有些不同的!在GridView中的BoundField新增了HtmlEncode 属性,且默认是true,这就使得DataFromatString失效!

    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: . . . . . . . 6/26/2004

    (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: . . . . . . . . 2004-06-26T20:11:04

    (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

    说明:
    String.Format
    将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。

    例子:

    int iVisit = 100;
    string szName = "Jackfled";
    Response.Write(String.Format("
    您的帐号是:{0} 。访问了 {1} .", szName, iVisit));

  • 相关阅读:
    【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
    微信卡券领取页面提示签名错误,微信卡券JSAPI签名校验工具对比签名一模一样,cardExt扩展字段有问题
    程序运行时动态生成缓存时存在的问题
    golang的beego框架开发时出现的问题纪录
    【备忘录】CentOS服务器mysql忘记root密码恢复
    试玩swoole扩展 第一天
    spring 定时任务执行2次
    JVM CUP占用率过高排除方法,windows环境
    spring cloud 服务A调用服务B自定义token消失,记录
    java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp
  • 原文地址:https://www.cnblogs.com/ceci/p/1943069.html
Copyright © 2011-2022 走看看