Formatting Fields
Each BoundField column provides a DataFormatString property that you can use to configure the
appearance of numbers and dates using a format string.
Format strings are generally made up of a placeholder and format indicator, which are
wrapped inside curly brackets. A typical format string looks something like this:
{0:C}
In this case, the 0 represents the value that will be formatted, and the letter indicates a predetermined
format style. In this case, C means currency format, which formats a number as a dollar
figure (so 3400.34 becomes $3,400.34). Here’s a column that uses this format string:
<asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C}" />
Table 10-3 shows some of the other formatting options for numeric values.
Table 10-3. Numeric Format Strings
Type Format String Example
Currency {0:C} $1,234.50
Brackets indicate negative values: ($1,234.50).
Currency sign is locale-specific: (?1,234.50).
Scientific (Exponential) {0:E} 1.234.50E+004
Percentage {0:P} 45.6%
Fixed Decimal {0:F?} Depends on the number of decimal places you
set. {0:F3} would be 123.400. {0:F0} would be 123.
You can find other examples in the MSDN Help. For date or time values, there is also an extensive
list. For example, if you want to write the BirthDate value in the format month/day/year (as in
12/30/05), you use the following column:
<asp:BoundField DataField="BirthDate" HeaderText="Birth Date"
DataFormatString="{0:MM/dd/yy}" />
Table 10-4 shows some more examples.
Table 10-4. Time and Date Format Strings
Type Format String Example
Short Date {0:d} M/d/yyyy
(for example: 10/30/2005)
Long Date {0:D} dddd, MMMM dd, yyyy
(for example: Monday, January 30, 2005)
Long Date and Short Time {0:f } dddd, MMMM dd, yyyy HH:mm aa
(for example: Monday, January 30, 2005
10:00 AM)
340 CHAPTER 10 ■ RICH DATA CONTROLS
Type Format String Example
Long Date and Long Time {0:F} dddd, MMMM dd, yyyy HH:mm:ss aa
(for example: Monday, January 30, 2005
10:00:23 AM)
ISO Sortable Standard {0:s} yyyy-MM-dd HH:mm:ss
(for example: 2005-01-30 10:00:23)
Month and Day {0:M} MMMM dd
(for example: January 30)
General {0:G} M/d/yyyy HH:mm:ss aa (depends on localespecific
settings)
(for example: 10/30/2002 10:00:23 AM)
The format characters are not specific to the GridView. You can use them with other controls,
with data-bound expressions in templates (as you’ll see later in this chapter), and as parameters for
many methods. For example, the Decimal and DateTime types expose their own ToString() methods
that accept a format string, allowing you to format values manually.
相关文章
- GridView模版列中设置
- GridView中,数据格式的设置种类
- yii2在GridView中设置ActionColumn的默认配置
- Gridview利用DataFormatString属性设置数据格式的方法
- 如何在android gridView中设置固定行数?
- 即使ReadOnly设置为false,也禁用ASP.NET GridView中的CheckBoxField列
- 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- GridView中动态设置CommandField是否可用或可见的小例子