30 March, 2010

C# DataFormatStrings

It is common practice to show data in GridViews in so many applications. Some times we have to format the incoming data such as currency, decimals and dates. To achieve this, we need to use "DataFormatString" property for GridColumn. There are some predefined format codes. They are as follows:

1. This will display a text Price: followed by the numeric text in currency format
Data Format String value : Price:
{0:C}
Applied to numeric and decimal data types.
The currency format is according to the culture info set in the web config file.

2. Integers are displayed in a zero-padded field four characters wide.
Data Format String value :
{0:D4}
Applied to integer datatype only.

3. To show two decimal places
Data Format String value :
{0:N2}
Applied to integer datatype only.

4. To round the numbers to one decimal place and are zero padded for numbers less than three digits.
Data Format String value :
{0:000.0}
Applied to numeric and decimal datatype only.

5. Long Date format
Data Format String value :
{0:D}
Applied to date and datetime datatype only.

6. Short Date format
Data Format String value : {0:d}
Applied to date and datetime datatype only.

7. Numeric Date format
Data Format String value :
{0:yy-MM-dd}
Applied to date and datetime datatype only.



No comments:

Post a Comment