This documentation is archived and is not being maintained.

BoundField.DataFormatString Property

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)

Not applicable.

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.

NoteNote:

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.

NoteNote:

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.

NoteNote:

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

Description

C

Displays numeric values in currency format.

D

Displays numeric values in decimal format.

E

Displays numeric values in scientific (exponential) format.

F

Displays numeric values in fixed format.

G

Displays numeric values in general format.

N

Displays numeric values in number format.

X

Displays numeric values in hexadecimal format.

NoteNote:

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#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>BoundField DataField Example</title>
</head>
<body>
    <form id="form1" 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>


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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Show: