Using the DataFormatString attribute to set the data format, dataformatstring
This example describes how to use the DataFormatString attribute to set the data format of Gridview. We will share this with you for your reference. The details are as follows:
First, set the AutoGenerateColumns attribute of the Gridview to False (the default value is False) and DataField to select the corresponding field. Note that you must set the HtmlEncode attribute of the field to False, otherwise, the format cannot be displayed. You can set the DataFormatString attribute of each field to output different formats.
The property Syntax of DataFormatString is as follows: {A: B}
For example, DataFormatString = "{0: Format String }"
The value before the colon (A in the general example) specifies the parameter index in the parameter list starting from scratch. This value can only be set to 0, because each cell has only one value.
The character after the colon (B in general) specifies the display format of the value. In addition, after the specified format symbol, you can specify the number of digits to be displayed in decimal places. For example, if the original data is "1.56" and the format is set to {0: N1}, the output is "1.5 」. The common numeric format is shown in the following table:
Format Character Description
C. The value is displayed 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.
The following are some examples for your reference:
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: yyyy-MM-dd
D. Detailed Date Format: yyyy-MM-dd
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
Finally, we will introduce another method to set the write time format. For example, {0: yyyy-MM-dd} will display the same format as {0: d, note that MM must be in upper case, because MM in upper case represents the month, while mm in lower case represents the minute in the time.
Format the GridView with DataFormatString
Display Data in the GridView. If the data to be displayed has multiple decimal places, you just want it to display only two decimal places. in delphi, you can simply use DisplayFormat. net, after checking msdn for half a day, I found that DataFormatString can be used to implement this function, but how to set it does not work. Finally, I found that because 2.0 is for security considerations, you must also set HtmlEncode = false to make DataFormatString take effect.
Leave a mark. You don't need to waste more time when using it next time.
Also, DataFormatString = "{0: F}" is the default format. It displays two decimal places. If the number of decimal places to be displayed is another value, DataFormatString = "{0: fn.
Example:
<Columns> <asp:BoundField DataField="EmployeeId" /> <asp:BoundField DataField="LastName" /> <asp:BoundField DataField="BirthDate" HtmlEncode ="false" DataFormatString="{0:yyyy-MM-dd}" />< /Columns>
I hope this article will help you design your asp.net program.
Articles you may be interested in:
- Use the ASP. NET GridView Control for column formatting time and DataFormatString
- Asp.net DataFormatString format the GridView
- How to bind ASP. NET data to the GridView Control
- The GridView Control for ASP. NET data binding
- Use jQuery and AJAX technology to regularly update a column of data in the GridView
- In asp.net, the displayed message is displayed when the cursor moves the GridView data.
- DevExpress implements the GridView to prompt messages when there is no data row
- Asp.net reads data from excel and binds it to the gridview
- Example of Using ajax to partially refresh the gridview for Data Binding
- C # use Excel to directly read data to the DataGridView
- Two Methods for js to obtain row data in the GridView
- C # connection with SQL: operations performed by the GridView control on the database
- Hover the mouse color in the asp.net Gridview data column