GridView method to format data using the DataFormatString property _ Practical Tips

Source: Internet
Author: User
Tags numeric value

The example in this article describes how the GridView uses the DataFormatString property to format data. Share to everyone for your reference, specific as follows:

First, set the AutoGenerateColumns property of the GridView to False (the default is False), DataField Select the appropriate field, especially to set the HTMLEncode property of the field you want to set to false. Otherwise, the format you set will not display, and you can set the DataFormatString property of each field to output different formats.

The DataFormatString property syntax is as follows: {A:B}

such as:dataformatstring= "{0: format string}"

The value before the colon (a in the general example) specifies the parameter index in the zero-based argument list. This value can only be set to 0 because there is only one value per cell.

The character after the colon (B in the general example) specifies the display format for the value. In addition, after the specified format symbol, you can specify the number of digits to display for the decimal. For example, the original data is "1.56", if the format is set to {0:n1}, then the output is "1.5". Its commonly used numerical formats are shown in the following table:

Format character description

C Displays the value in currency format.

D displays numeric values in decimal format.

E displays values in scientific notation (exponential) format.

F displays values in a fixed format.

G displays numeric values in a regular format.

N displays numeric values in numeric format.

X Displays the numeric value in hexadecimal format.

Here are some examples for you to refer to:

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

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

Format Description Output format
D Compact Date format YYYY-MM-DD
D Detail date format yyyy year mm month DD Day
F Full Format (long date + short time) dddd, MMMM dd, yyyy hh:mm
F full DateTime 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 time format Yyyy-mm-dd HH:MM:SS
T streamlined time format hh:mm
T Verbose time Format HH:mm:ss

Finally, the introduction of another method of setting, that is, direct write time format, such as {0:YYYY-MM-DD} will display the same format as {0:d}, note that mm must be uppercase, because the MM uppercase is the month, and mm lowercase in the time of the Minutes.

Format the GridView with DataFormatString

In the GridView display of data, to display the data there are a lot of decimal places, want it to show only two decimal, in Delphi, directly with DisplayFormat on the line, in. NET, check for a half-day MSDN, found using dataformatstring can achieve this function, but how to set no effect, finally found that, because 2.0 for security considerations, but also set HTMLEncode = False, can make dataformatstring effective .

Leave a mark, the next time you use, you don't have to waste more than n time.

Also,dataformatstring = "{0:f}", which is the default format, displays two decimal places , anddataformatstring = "{0:FN}" If you need to display a number of decimal places for other values.

Cases:

<Columns>
    <asp:boundfield datafield= "EmployeeId"/>
    <asp:boundfield datafield= "LastName" >
    <asp:boundfield datafield= "birthdate" HtmlEncode = "false" dataformatstring= "{0:yyyy-mm-dd}"/>
</columns>

I hope this article will help you to ASP.net program design.

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.