Data format setting expression
. NET Framework format setting expression, which is applied to data before data is displayed in the column.
This expression consists of optional static text and format specifiers in the following format:
{0: format specifier}
0 indicates the data element to be formatted in the column. Therefore, zero is usually used to indicate the first (and unique) element.
Format specifier is preceded by a colon (:), which consists of one or more letters indicating how to format the data.
The format specifiers that can be used depend on the data type to be formatted: date, number, or other type.
The following table shows an example of formatting expressions for different data types. For more information about formatting expressions, see formatting types.
Format setting expression |
Apply to this data type |
Description |
Price: {0: c} |
Numeric/decimal |
Display"Price :"Followed by a number in the currency format. The currency format depends onPageCommand orWeb. configFile. |
{0: D4} |
Integer(It cannot be used with decimals .) |
An integer is displayed in the fields with four character widths filled with zero. |
{0: N2} % |
Numeric |
Displays the digits precise to the decimal point, followed"%". |
{0: 000. 0} |
Numeric/decimal |
Number rounded to the decimal place. Less than three digits are filled with zero. |
{0: d} |
Date/datetime |
Long Date Format ("Thursday, August 06,199 6"). The date format depends on the page orWeb. configFile. |
{0: d} |
Date/datetime |
Short Date Format ("12/31/99"). |
{0: yy-mm-dd} |
Date/datetime |
date represented by the number year-month-day ( 96-08-06 ). |
Example of Setting data format in ASP. NET:
{0: d} YY-MM-DD
{0: p} % 00.00%
{0: N2} 12.68
{0: N0} 13
{0: C2 }$ 12.68
{0: d} 3/23/2003
{0: t} 12:00:00 AM
{0: Male; female}
Format -- dataformatstring of the DataGrid Data Format
Dataformatstring = "{0: Format String }"
If the original data is "12.34" and the format is set to {0: N1}, the output is "12.3 」
Format String data 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
Datetime. Now. tostring ("yyyy-mm-dd"); // The Best Mode
Aspx:
<Asp: DataGrid id = "datagrid1" runat = "server" autogeneratecolumns = "false" width = "204px">
<Columns>
<Asp: boundcolumn datafield = "date" dataformatstring = "{0: yyyy-mm-dd}"> </ASP: boundcolumn>
</ASP: DataGrid>
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 usedDataformatstringAttribute.
<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!
String. FormatFormat result
String. Format
(C) Currency:... ($123.00)
(D) decimal: ......-123
(E) Scientific:...-1.234500e + 002
(F) Fixed Point:...-123.45
(G) General:...-123
(N) number:...-123.00
(P) percent: ......-12,345.00%
(R) round-trip: .........-123.45
(X) hexadecimal: ...... ffffff85
(D) Short Date :....... 6/26/2004
(D) Long Date:... Saturday, June 26,200 4
(T) Short Time :...
(T) long time: ...... 8:11:04
(F) Full date/short time: .. Saturday, June 26,200 4 pm
(F) Full date/long time:... Saturday, June 26,200 4 8:11:04
(G) General Date/short time:. 6/26/2004 8: 11 pm
(G) General Date/long time:. 6/26/2004 8:11:04
(M) month :...
(R) rfc1123:... sat, 26 Jun 2004 20:11:04 GMT
(S) sortable :........ 2004-06-26 T20: 11: 04
(U) Universal sortable:... 2004-06-26 20: 11: 04z (invariant)
(U) Universal sortable:... Sunday, June 27,200 4 3:11:04 AM
(Y) year:... June, 2004
(G) General:... green
(F) flags: ...... Green (flags or integer)
(D) decimal number:... 3
(X) hexadecimal: ....... 00000003
Note:
String. Format
StringReplace each format item with the text equivalent of the value of the corresponding object.
Example:
Int ivisit= 100;
String szname = "jackfled ";
Response. Write (string. Format ("Your account is:{0}. Accessed{1}Times. ", Szname, ivisit ));
trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1789288