Formatting Columns in an ASP.Net GridView Control

Tue, Jun 23, 2009

Tech Tips

To format columns in a GridView control, you can use the FormatString property:

  1. //Formatting Currency  
  2. <asp:GridView id="grid" Runat="Server" >  
  3. <Columns>  
  4.         <asp:BoundField HeaderText="Amount" DataField="amt"   
  5.          FormatString="{0:c}"/>  
  6. </Columns>  
  7. </asp:GridView>  

To format a Date column, you must also set the HTMLEncode property to “False”.

  1. //Formatting a date column  
  2. <asp:BoundField HeaderText="Amount" DataField="date1"   
  3.  FormatString="{0:d}" HTMLEncode="False" />  

To format a Currency with no cents:

  1. //No cents in the currency  
  2. <asp:BoundField HeaderText="Amount" DataField="amt"   
  3.  FormatString="{0:$#,0}" />  
Bookmark and Share

One Response to “Formatting Columns in an ASP.Net GridView Control”

  1. mutuelle Says:

    Merci pour ce post intéressant. si vous continuer ainsi je vais devenir un lecteur fidéle