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