Why dataformatstring is invalid in gridview

Source: Internet
Author: User

Today, when I helped a customer deploy a website, I found that there were several differences in the website that was previously well tuned locally, because local development was in the vs 2010 environment, the configuration of the customer space and the domain name is different from that of the local environment;

One problem is that dataformatstring in the gridview is invalid;

Solution:

 

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. At this time, we used Dataformatstring Attribute.

<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 exampleCodeThe date and currency binding modes are displayed.DataformatstringIn{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. In the gridviewASP:BoundfieldUseDataformatstringAttribute must be setHtmlencode= "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 format stringDataformatstring="¥{0: c }".

Summary:
Using datafromatstring in the gridview is somewhat different from using it in the DataGrid! The htmlencode attribute is added to boundfield in the gridview and the default value is true, which invalidates datafromatstring!

 

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.