[ASP. NET] How to Use datafromatstring in the gridview

Source: Internet
Author: User

When we obtain data entities from the business logic layer, the next thing is to bind them to the control. Some fields in the data object can be directly bound to the interface, but some fields need to be reformatted. For example, for the currency unit field, you need to display the currency symbol and the display separator at every three places; for example, for the date field, the database stores the date and time, however, the page is displayed in the format of xxxx, XX, and XX. In this case, the dataformatstring attribute is used.

<Asp: gridview id = "grvresult" runat = "server" autogeneratecolumns = "false" width = "100%">

<Columns>

<Asp: boundfield headertext = "Reservation date" datafield = "operationdate"Dataformatstring = "{0: yyyy-mm-dd}" htmlencode = "false">

</ASP: boundfield>

<Asp: boundfield headertext = "total orders" datafield = "totalrate"Dataformatstring = "{0: c}" htmlencode = "false">

</ASP: boundfield>

</Columns>

</ASP: gridview>

For example, the above Code shows the date and currency binding methods. In dataformatstring{0}It is a fixed format, which is used with {0} In string. fromat ("{0}", somestring) to indicate the parameter index number of the bound context. Then, add a formatted string to the end. For detailed usage instructions, refer to msdn.

Note the following points:
1. asp: boundfield in the gridview must set attributes when using dataformatstringHtmlencode = "false"Otherwise, it does not work.
2. If you want to use a formatted string of the date type, the corresponding fields in the data object must also be of the date type.
3. Format String C to indicate the currency unit. The data type to bind should be numeric. If the string type does not work, you need to manually add the formatted string as dataformatstring = "¥ {0: c }".

V

Related Article

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.