Search Amazon:

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>

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

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

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms