BoundField.DataFormatString Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets the string that specifies the display format for the value of the field.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual string DataFormatString { get; set; }
/** @property */
public String get_DataFormatString ()

/** @property */
public void set_DataFormatString (String value)

public function get DataFormatString () : String

public function set DataFormatString (value : String)

Property Value

A formatting string that specifies the display format for the value of the field. The default is an empty string (""), which indicates that no special formatting is applied to the field value.

Use the DataFormatString property to specify a custom display format for the values displayed in the BoundField object. If the DataFormatString property is not set, the field's value is displayed without any special formatting.


When the HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied. For some objects, such as dates, you might want to control how the object is displayed with a formatting string. In those cases, you must set the HtmlEncode property to false.

By default, the formatting string is applied to the field value only when the data-bound control that contains the BoundField object is in read-only mode. To apply the formatting string to field values while in edit mode, set the ApplyFormatInEditMode property to true.

The formatting string can be any literal string and usually includes a placeholder for the field's value. For example, in the formatting string "Item Value: {0}", the field's value is substituted for the {0} placeholder when the string is displayed in the BoundField object. The rest of the formatting string is displayed as literal text.


If the formatting string does not include a placeholder, the field's value from the data source is not included in the final display text.

The placeholder consists of two parts, separated by a colon and wrapped in braces, in the form {A : Bxx}. The value before the colon (A in the general example) specifies the field value's index in a zero-based list of parameters.


This parameter is part of the formatting syntax. Because there is only one field value in each cell, this value can only be set to 0.

The colon and the values after the colon are optional. The character after the colon (B in the general example) specifies the format to display the value in. The following table lists the common formats.

Format character



Displays numeric values in currency format.


Displays numeric values in decimal format.


Displays numeric values in scientific (exponential) format.


Displays numeric values in fixed format.


Displays numeric values in general format.


Displays numeric values in number format.


Displays numeric values in hexadecimal format.


Format characters are not case-sensitive, except for X, which displays the hexadecimal characters in the case specified.

The value after the format character (xx in the general example) specifies the number of significant digits or decimal places to display. For example, the formatting string "{0:F2}" displays a fixed-point number with two decimal places.

For more information on formatting strings, see Formatting Overview.

The following code example demonstrates how to use the DataFormatString property to specify a custom display format for the values of a field. The numeric values in the field are formatted to display four decimal places.

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

    <form runat="server">
      <h3>BoundField DataField Example</h3>

      <asp:gridview id="DiscountsGridView" 
          <asp:boundfield datafield="discounttype"
            headertext="Discount Type"/>                    
          <asp:boundfield datafield="discount"
      <!-- 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%>"

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions