Format data in a bound DataGrid in ASP.NET

The DataGrid's BoundColumn control lets the data displayed be formatted via the DataFormatString property. The DataFormatString property is a string property that gets assigned a value using the syntax: {0:Format}. The formatting that can be applied to the field displayed by the BoundColumn depends on the type of the field.

The following date formatting values can be used:

Format PatternNameExample
d Short date 8/27/1989
D Long date Sunday, August 27, 1989
t Short time 3:32 PM
T Long time 3:32:00 PM
f Full date/time
(short time)
Sunday, August 27, 1989 3:32 PM
F Full date/time
(long time)
Sunday, August 27, 1989 3:32:00 PM
g General date/time
(short time)
8/27/1989 3:32 PM
G General date/time
(long time)
8/27/1989 3:32:00 PM
m or M Month day August 27
r or R RFC 1123 Sun, 27 Aug 1989 8:32:00 GMT
s Sortable date/time 1989-08-27T15:32:00
u Universable sortable date/time 1989-08-27 15:32:00z
U Universable sortable date/time Sunday, August 27, 1989 11:32:00 PM
y or Y Year month August, 1989

Example: format a DateTime field without the time.

   <asp:DataGrid runat="server" AutoGenerateColumns="False" ...>
      <Columns>
         ...
      <asp:BoundColumn DataField="MyDateColumn" DataFormatString="{0:d}" ... />
         ...
      </Columns>
   </asp:DataGrid>

The following numeric formatting values can be used:

Format Pattern Name
C Curency format
D Decimal format
E Scientific (exponential) format
F Fixed format
G General format
N Number format
X Hexidecimal format

Example: Format a currency column:

   <asp:DataGrid runat="server" AutoGenerateColumns="False" ...>
      <Columns>
         ...
      <asp:BoundColumn DataField="MyCurrencyColumn" DataFormatString="{0:c}" ... />
         ...
      </Columns>
   </asp:DataGrid>


About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2016 TheScarms
Goto top of page