Introduction to DataFormatString for data binding

  • 2020-07-21 07:23:39
  • OfStack

DataFormatString is a property that many Asp. Net controls have, such as GridView, and so on.

The DataFormatString attribute syntax is as follows:

DataFormatString="{0: format string}"

We know that in DataFormatString {0}
Represents the data itself, and the format string after the colon represents the format in which we want the data to appear. You can also specify the number of digits the decimal should display after the specified format symbol. For example, the original data is
"1.56", output "1.5" if the format is {0:N1}.

DataFormatString setting BoundField usually has the following types
DataFormatString= "{0:C}" currency whose format depends on the current Culture setting in Thread
DataFormatString= "{0:E}" scientific counting method
DataFormatString= "{0: P}" percentage
DataFormatString = "{0: F? }" several places after the decimal point
DataFormatString= "{0:d}" M/d/yyyy as 10/30/2008
DataFormatString= "{0:f}" long date, short time. dddd,MMMM dd,yyyy HH:mm aa e.g., Monday, January30, 2008 10:00am
DataFormatString= "{0: D}" long date. dddd,MMMM dd,yyyy e.g. Monday, January 30,2008
DataFormatString= "{0:F}" long date, long time dddd,MMMM dd,yyyy HH: ss aa
DataFormatString= "{0: s}" ISO standard time. yyyy MM - ddTHH: mm: ss, such as: the 2008-01-30 T10:20:55 am
DataFormatString= "{0:M}" Month and date MMMM dd, e.g. January30
DataFormatString= "{0:G}" 1 General format M/d/yyyy HH: ss aa
-----------------------------------------------------------------
Format string input results
"{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
Commonly used date and time format:
Format description Output format
d Streamlined 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
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
Month format MMMM dd
s Moderate date and time format ES191en-ES192en-ES193en HH:mm:ss
t Simplified time format HH:mm
T detailed time format HH:mm:ss

< %# String.Format("{0:yyyy-MM-dd} ", Eval("EffectiveDate "))% >
< %# String.Format("{0:yyyy-M-d} ", DataBinder.Eval(Container.DataItem, "EffectiveDate "))% >

Related articles: