Format data in asp.net using BoundColumn.DataFormatString Property

Syntax

 

Inline

<asp:boundcolumn dataformatstring = strFormat … >

Script

BoundColumn.DataFormatString = strFormat

 

Property Value

 

strFormat

A formatting string that specifies the display format of data in the column.

The property is read/write with no default value.

Remarks

Use the DataFormatString property to specify the display format for data in the column.

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.

Example

The following example demonstrates how to use the DataFormatString property to specify currency format for the column that displays prices in a DataGrid control.

<asp:boundcolumn headertext = "Price"    datafield = "Price"    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

 

SOURCE: http://authors.aspalliance.com/aspxtreme/sys/Web/UI/WebControls/BoundColumnClassDataFormatString.aspx

One Comment

  1. Posted March 25, 2008 at 4:28 am | Permalink

    Interesting page.., dude


Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*