GridViewBoundField.DataFormatString属性

来源:互联网 发布:呼叫死软件 编辑:程序博客网 时间:2024/05/19 13:22

价格:{0:¥#,##0.00}
数字:{0:0.00}
建立时间:{0:yyyy年MM月dd日 hh时mm分ss秒}

 

获取或设置字符串,该字符串指定字段值的显示格式。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

Collapse 图像语法

Visual Basic(声明)

Public Overridable Property DataFormatString As String

 

Visual Basic(用法)

Dim instance As BoundField

Dim value As String

 

value = instance.DataFormatString

 

instance.DataFormatString = value

 

C#

public virtual string DataFormatString { get; set; }

 

C++

public:

virtual property String^ DataFormatString {

       String^ get ();

       void set (String^ value);

}

 

J#

/** @property */

public String get_DataFormatString ()

 

/** @property */

public void set_DataFormatString (String value)

 

JScript

public function get DataFormatString () : String

 

public function set DataFormatString (value : String)

属性值

指定字段值的显示格式的格式化字符串。默认值为空字符串 (""),表示尚无特殊格式设置应用于该字段值。

Collapse 图像备注

使用 DataFormatString 属性为 BoundField 对象中显示的值指定自定义显示格式。如果未设置 DataFormatString 属性,则字段的值在显示时不使用任何特殊的格式设置。默认情况下,只有当包含 BoundField 对象的数据绑定控件处于只读模式时,格式化字符串才应用到字段值。若要在编辑模式中将格式化字符串应用到字段值,请将 ApplyFormatInEditMode 属性设置为 true

格式化字符串可以为任意字符串,并且通常包含字段值的占位符。例如,在格式化字符串“Item Value: {0}”中,当 BoundField 对象中显示字符串时,字段的值会代替 {0} 占位符。格式化字符串的剩余部分显示为文本。

Note注意

如果格式化字符串不包含占位符,则来自数据源的字段值将不包含在最终显示文本中。

占位符由用冒号分隔的两部分组成并用大括号括起,格式为 {A : Bxx}。冒号前的值(常规示例中为 A)指定在从零开始的参数列表中的字段值的索引。

Note注意

此参数是格式化语法的一部分。因为每个单元格中只有一个字段值,所以这个值只能设置为 0

该冒号以及冒号后面的值是可选的。冒号后的字符(常规示例中为 B)指定值的显示格式。下表列出了一些常用格式。

格式字符

说明

C

以货币格式显示数值。

D

以十进制格式显示数值。

E

以科学记数法(指数)格式显示数值。

F

以固定格式显示数值。

G

以常规格式显示数值。

N

以数字格式显示数值。

X

以十六进制格式显示数值。

Note注意

格式字符不区分大小写,但 X 除外,它以指定的大小写形式显示十六进制字符。

    

格式字符后的值(常规示例中为 xx)指定显示的值的有效位数或小数位数。例如,格式化字符串“{0:F2}”将显示带两位小数的定点数。

有关格式化字符串的更多信息,请参见格式化概述。

Collapse 图像示例

下面的代码示例演示如何使用 DataFormatString 属性为字段值指定自定义显示格式。字段中的数值被格式化为显示四个小数位。

Visual Basic 

CopyCode image复制代码

<%@ Page language="VB" %>

 

<html>

  <body>

    <form runat="server">

       

      <h3>BoundField DataField Example</h3>

 

      <asp:gridview id="DiscountsGridView"

        datasourceid="DiscountsSqlDataSource"

        autogeneratecolumns="False"

        runat="server">

                

        <columns>           

          <asp:boundfield datafield="discounttype"

            headertext="Discount Type"/>                   

          <asp:boundfield datafield="discount"

            dataformatstring="{0:F4}%"

            itemstyle-horizontalalign="Right" 

            headertext="Discount"/>               

        </columns>

               

      </asp:gridview>

           

      <!-- This example uses Microsoft SQL Server and connects -->

      <!-- to the Pubs sample database.                        -->

      <asp:sqldatasource id="DiscountsSqlDataSource" 

        selectcommand="SELECT [discounttype], [stor_id], [lowqty], [highqty], [discount] FROM [discounts]"

        connectionstring="<%$ ConnectionStrings:PubsConnectionString%>"

        runat="server">

      </asp:sqldatasource>

           

    </form>

  </body>

</html>

 

 

C# 

CopyCode image复制代码

<%@ Page language="C#" %>

 

<html>

  <body>

    <form runat="server">

       

      <h3>BoundField DataField Example</h3>

 

      <asp:gridview id="DiscountsGridView"

        datasourceid="DiscountsSqlDataSource"

        autogeneratecolumns="False"

        runat="server">

               

        <columns>

          <asp:boundfield datafield="discounttype"

            headertext="Discount Type"/>                   

          <asp:boundfield datafield="discount"

            dataformatstring="{0:F4}%"

            itemstyle-horizontalalign="Right" 

            headertext="Discount"/>               

        </columns>

               

      </asp:gridview>

           

      <!-- This example uses Microsoft SQL Server and connects -->

      <!-- to the Pubs sample database.                        -->

      <asp:sqldatasource id="DiscountsSqlDataSource" 

        selectcommand="SELECT [discounttype], [stor_id], [lowqty], [highqty], [discount] FROM [discounts]"

        connectionstring="<%$ ConnectionStrings:PubsConnectionString%>"

        runat="server">

      </asp:sqldatasource>

           

    </form>

  </body>

</html>

 

Collapse 图像平台

Windows 98Windows 2000 SP4Windows Server 2003Windows XP Media Center EditionWindows XP Professional x64 EditionWindows XP SP2Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

Collapse 图像版本信息

.NET Framework

受以下版本支持:2.0

Collapse 图像请参见

参考

BoundField 
BoundField 成员
System.Web.UI.WebControls 命名空间
BoundField.DataField 属性
BoundField.ApplyFormatInEditMode 属性

其他资源

格式化概述