Method of Setting Data Format by Gridview Using DataFormatString Attribute

  • 2021-07-13 05:03:45
  • OfStack

In this paper, an example is given to describe the method of setting data format by Gridview using DataFormatString attributes. Share it for your reference, as follows:

First, set the AutoGenerateColumns attribute of Gridview to False (False by default), and select the corresponding field for DataField. Special attention should be paid to setting the HtmlEncode attribute of the field that needs to be set to False, otherwise the set format will not be displayed, and then you can set the DataFormatString attribute of each field to output different formats.

The syntax for the DataFormatString attribute is as follows: {A: B}

Such as: DataFormatString= "{0: format string}"

The value before the colon (A in the general example) specifies the parameter index in the zero-based parameter list. This value can only be set to 0 because there is only 1 value in each cell.

The character after the colon (B in the general example) specifies the display format of the value. In addition, after the specified format symbol, you can specify the number of digits to display the decimal. For example, the original data is "1.56", and if the format is set to {0: N1}, the output is "1.5". Its common numerical format is shown in the following table:

Format character description

C displays values in currency format.

D displays values in decimal format.

E displays values in scientific notation (exponential) format.

F displays numeric values in a fixed format.

G displays numeric values in a regular format.

N displays numeric values in numeric format.

X displays values in 106-ary format.

Here 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

Its common date format is shown in the following table:

Format description output format
d Reduced Date Format yyyy-MM-dd
D Detailed Date Format yyyy Year MM Month dd Day
f Full Format (long date + short time) dddd, MMMM dd, yyyy HH: mm
F Full Date Time Format (long date + long time) dddd, MMMM dd, yyyy HH: mm: ss
g 1 General Format (short date + short time) MM/dd/yyyy HH: mm
G 1 General Format (short date + long time) MM/dd/yyyy HH: mm: ss
m, M month-day format MMMM dd
s Moderate Date-Time Format yyyy-MM-dd HH: mm: ss
t Reduced Time Format HH: mm
T verbose time format HH: mm: ss

Finally, another way to set it is to write the time format directly. For example, {0: yyyy-MM-dd} will display the same format as {0: d}. Note that MM must be uppercase, because MM uppercase represents the month, while mm lowercase represents the minutes in time.

Formatting GridView with DataFormatString

Display data in GridView, and the data to be displayed has many decimal places, so I want it to display only two decimal places. In delphi, just use DisplayFormat directly. In. net, check msdn for half a day, and find that this function can be realized by using DataFormatString, but how to set it doesn't work. Finally, it is found that due to security considerations of 2.0, it is necessary to set HtmlEncode = false at the same time to make DataFormatString take effect.

Leave a mark, and when you use it next time, you don't have to waste more time on N.

Also, DataFormatString = "{0: F}", which is the default format, displays two decimal places, and DataFormatString = "{0: Fn}" if the decimal places to be displayed are other values.

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 is helpful to everyone's asp. net programming.


Related articles: