System.Web.UI.WebControls Namespace BoundField Class
.NET Framework version 2.0
Sets or retrieves the string that specifies the display format of data for a BoundField.
Inline |
<asp:boundfield dataformatstring = strFormat ... > |
Script |
BoundField.DataFormatString = strFormat |
strFormat |
A formatting string that specifies the display format of data in a BoundField. |
The property is read/write with no default value.
Use the DataFormatString property to specify a custom display format for data in a BoundField control.
By default, the formatting string is applied to the field value only when the parent control that contains the BoundField is in read-only mode. To apply the formatting string to field values while in edit mode, set the ApplyFormatInEditMode property to true.
The data format string consists of two parts, separated by a colon and wrapped in braces, in the form
{ A:Bxx }
A specifies the parameter index in a zero-based list of parameters. This value can only be "0" since there is only one value in each cell. B specifies the format to display the value according to the following table:
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. |
The formatting character is not case sensitive except for "X", which displays the hexidecimal characters in the case specified.
xx specifies the number of significant digits or decimal places to display the value.
For example, the formatting string {0:D2} would format the cell to display a number with two decimal places.
NOTE: The entire string must be enclosed in curly brackets to denote that this is a format string and not a literal string. Any text outside of the curly brackets is displayed as literal text.
For more information on formatting strings, see Formatting Base Types.
The following example demonstrates how to use the DataFormatString property to specify currency format for the column that displays prices in a GridView control.
<asp:boundfield headertext = "Price"
datafield = "Price"
htmlencode=false
dataformatstring = "{0:c}"
itemstyle-horizontalalign = "right" />
Show me
The following table contains examples of formatting strings and the results. The example assumes standard English settings.
Numeric Value |
Format String |
Result |
12345.6789 |
"{0:C}" |
$12,345.68 |
-12345.6789 |
"{0:C}" |
( $12,345.68 ) |
12345 |
"{0:D}" |
12345 |
12345 |
"{0:D8}" |
00012345 |
12345.6789 |
"{0:E}" |
1234568E+004 |
12345.6789 |
"{0:E10}" |
1.2345678900E+004 |
12345.6789 |
"{0:F}" |
12345.68 |
12345.6789 |
"{0:F0}" |
12346 |
12345.6789 |
"{0:G}" |
12345.6789 |
123456789 |
"{0:G7}" |
1.234568E8 |
12345.6789 |
"{0:N}" |
12,345.68 |
123456789 |
"{0:N4}" |
123,456,789.0000 |
12345.6789 |
"Total: {0:C}" |
Total: $12345.68 |
BoundField Members