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 display the separator at every three places. 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: You must set attributes when using dataformatstring in ASP: boundfield in the gridview.Htmlencode = "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 the 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 }".
Use the dataformatstring attribute to provide the custom formats of items in the column.
Gets or sets the display format strings of items in the specified column. Specifies the formatted string of the display format of each item in the column. The default value is string. Empty.
A data format string consists of two parts separated by a colon, in the form of {A: bxx }. For example, the formatted string {0: F2} displays the number of fixed points with two decimal places.
The entire string must be placed in braces, indicating that it is a format string, not the actual string. Any text outside the braces is displayed as the actual text.
The value before the colon specifies the parameter index in the parameter list starting from scratch. The value can only be set to 0, because each cell has only one value.
The display format of the specified value after the colon. The following table lists some common formats.
Format Character Description
C. display the value in currency format
D. The value is displayed in decimal format.
E. The value is displayed in the scientific notation (exponent) format.
F displays the value in a fixed format.
G displays the value in the regular format.
N is a numeric value.
X indicates the value in hexadecimal format.
Msdn example:
<Asp: boundcolumn headertext = "item" datafield = "item" readonly = "true"/> <asp: boundcolumn headertext = "quantity" datafield = "QTY" dataformatstring = "{0: N0}"/> <asp: boundcolumn headertext = "price" datafield = "price" dataformatstring = "{0: c}"/> <asp: boundcolumn headertext = "weight" datafield = "weight" readonly = "true" dataformatstring = "{0: F3}"/> <asp: boundcolumn headertext = "expires" datafield = "expires" readonly = "true" dataformatstring = "{0: g}"/>
Format Date: dataformatstring = "{0: yyyy-mm-dd }"
Format time: dataformatstring = "{0: hh: mm: SS }"
Note: The upper-case M is month, and the lower-case M is minute. Note the differences between the upper-case m and the lower-case M. This is the case when using C.
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
The commonly used date formats are shown in the following table:
Format description output format
D. Simplified Date Format: mm/DD/YYYY
D detailed Date Format: dddd, Mmmm DD, yyyy
F full format (long date + short time) dddd, Mmmm DD, yyyy hh: mm
F complete Date and Time 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 and Time Format: yyyy-mm-dd hh: mm: SS
T simplified time format hh: mm
T detailed time format hh: mm: SS
<Asp: boundfield datafield = "email"
Dataformatstring = "<a href = 'mailto: {0} '> {0} </a>"
Headertext = "email"
Sortexpression = "email"
Htmlencode = "false"/>
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 display the separator at every three places. 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: You must set attributes when using dataformatstring in ASP: boundfield in the gridview.Htmlencode = "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 the 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 }".
Use the dataformatstring attribute to provide the custom formats of items in the column.
Gets or sets the display format strings of items in the specified column. Specifies the formatted string of the display format of each item in the column. The default value is string. Empty.
A data format string consists of two parts separated by a colon, in the form of {A: bxx }. For example, the formatted string {0: F2} displays the number of fixed points with two decimal places.
The entire string must be placed in braces, indicating that it is a format string, not the actual string. Any text outside the braces is displayed as the actual text.
The value before the colon specifies the parameter index in the parameter list starting from scratch. The value can only be set to 0, because each cell has only one value.
The display format of the specified value after the colon. The following table lists some common formats.
Format Character Description
C. display the value in currency format
D. The value is displayed in decimal format.
E. The value is displayed in the scientific notation (exponent) format.
F displays the value in a fixed format.
G displays the value in the regular format.
N is a numeric value.
X indicates the value in hexadecimal format.
Msdn example:
<Asp: boundcolumn headertext = "item" datafield = "item" readonly = "true"/> <asp: boundcolumn headertext = "quantity" datafield = "QTY" dataformatstring = "{0: N0}"/> <asp: boundcolumn headertext = "price" datafield = "price" dataformatstring = "{0: c}"/> <asp: boundcolumn headertext = "weight" datafield = "weight" readonly = "true" dataformatstring = "{0: F3}"/> <asp: boundcolumn headertext = "expires" datafield = "expires" readonly = "true" dataformatstring = "{0: g}"/>
Format Date: dataformatstring = "{0: yyyy-mm-dd }"
Format time: dataformatstring = "{0: hh: mm: SS }"
Note: The upper-case M is month, and the lower-case M is minute. Note the differences between the upper-case m and the lower-case M. This is the case when using C.
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
The commonly used date formats are shown in the following table:
Format description output format
D. Simplified Date Format: mm/DD/YYYY
D detailed Date Format: dddd, Mmmm DD, yyyy
F full format (long date + short time) dddd, Mmmm DD, yyyy hh: mm
F complete Date and Time 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 and Time Format: yyyy-mm-dd hh: mm: SS
T simplified time format hh: mm
T detailed time format hh: mm: SS
<Asp: boundfield datafield = "email"
Dataformatstring = "<a href = 'mailto: {0} '> {0} </a>"
Headertext = "email"
Sortexpression = "email"
Htmlencode = "false"/>