Gridview uses the dataformatstring attribute to set the data format

Source: Internet
Author: User

First, set the autogeneratecolumns attribute of the gridview to false (the default value is false), and select the corresponding field in datafield, note that you must set the htmlencode attribute of the field to false. Otherwise, the format cannot be displayed, then you can set the dataformatstring attribute of each field to output different formats.

The property Syntax of dataformatstring is as follows: {A: B}

For example, dataformatstring = "{0: Format String }"

The value before the colon (A in the general example) specifies the parameter index in the parameter list starting from scratch. This value can only be set to 0, because each cell has only one value.

The character after the colon (B in general) specifies the display format of the value. In addition, after the specified format symbol, you can specify the number of digits to be displayed in decimal places. For example, if the original data is "1.56" and the format is set to {0: N1}, the output is "1.5 」. The common numeric format is shown in the following table:

Format Character Description

C
The value is displayed in currency format.
 
D
The value is displayed in decimal format.
 
E
The value is displayed in the scientific notation (INDEX) format.
 
F
The value is displayed in a fixed format.
 
G
The value is displayed in the regular format.
 
N
The value is displayed in numeric format.
 
X
The value is displayed in hexadecimal format.

The following are some examples for your reference:
Format String input result
"{0: c}" 12345.6789 $12,345.68
"{0: c}"-12345.6789 ($12,345.68)
"{0: d}" 12345 12345
"{0: D8}" 12345 00012345
"{0: e}" 12345.6789 1234568e + 004
"{0: E10}" 12345.6789 1.2345678900e + 004
"{0: F }" 12345.6789 12345.68
"{0: F0 }" 12345.6789 12346
"{0: g}" 12345.6789 12345.6789
"{0: G7}" 123456789 1.234568e8
"{0: n}" 12345.6789 12,345.68
"{0: N4}" 123456789 123,456,789.0000
"Total: {0: c}" 12345.6789 total: $12345.68

The commonly used date formats are shown in the following table:

Format description output format
D. Simplified Date Format: yyyy-mm-dd
D. Detailed Date Format: yyyy-mm-dd
F full format (long date + short time) dddd, Mmmm DD, yyyy hh: mm
F complete Date and Time Format (long date + long time) dddd, Mmmm DD, yyyy hh: mm: SS
G General Format (short date + short time) mm/DD/YYYY hh: mm
G General Format (short date + long time) mm/DD/YYYY hh: mm: SS
M, M month/day format mmmm dd
S moderate Date and Time Format: yyyy-mm-dd hh: mm: SS
T simplified time format hh: mm
T detailed time format hh: mm: SS

Finally, we will introduce another method to set the write time format. For example, {0: yyyy-mm-dd} will display the same format as {0: d, note that mm must be in upper case, because mm in upper case represents the month, while mm in lower case represents the minute in the time.

Format the gridview with dataformatstring

Display Data in the gridview. If the data to be displayed has multiple decimal places, you just want it to display only two decimal places. in Delphi, you can simply use displayformat. net, I checked the msdn for half a day and found thatDataformatstring Yes, this function can be implemented, but how to set it does not work. Finally, we found that because 2.0 is for security consideration, we also need to set it at the same time. Htmlencode = false to make dataformatstring take effect . Leave a mark. You don't have to waste more time when you use it next time. And, Dataformatstring = "{0: f}", which is the default format and displays two decimal places If the number of decimal places to be displayed is another value, Dataformatstring = "{0: fn }" You can. For example: <columns>
<Asp: boundfield datafield = "employeeid"/>
<Asp: boundfield datafield = "lastname"/>
<Asp: boundfield datafield = "birthdate"Htmlencode = "false" Dataformatstring = "{0: yyyy-mm-dd}"/>
</Columns>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.