Introduction of dataformatstring attribute syntax and solution to dataformatstring invalid problem

Source: Internet
Author: User
DataFormatString is a lot of asp.net controls have properties, such as the GridView, and so on, the following briefly describes this property.
The DataFormatString property syntax is as follows:

Dataformatstring= ' {0: format string} '

We know that {0} in DataFormatString
Represents the data itself, and the format string after the colon represents the format in which you want the data to appear, and you can specify the number of digits to display for a decimal after the specified format symbol. For example, the original data is
"1.56", if the format is set to {0:n1}, the output is "1.5". Its commonly used numerical formats are shown in the following table:



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 mm/dd/yyyy

D Detail Date format dddd, MMMM DD, yyyy

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

Knowing that there are other uses, such as having a datetime type of data that you want to show in your own mind, you can call its ToString () method, and don't believe you can try the following statement:
DateTime Now=datetime.now; Console.WriteLine (now. ToString ("G"));
Console.WriteLine (now. ToString ("D"));
Console.WriteLine (now. ToString ("S"));
Console.WriteLine (now. ToString ("T"));
Console.WriteLine (now. ToString ("F")); ....... More usage is left to the reader to explore. Finally special note: In the asp.net under the GridView dataformatstring invalid problem, you need to set the HTMLEncode property of the field, the default is true, change it to false, the following figure:

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.