:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  

Datagrid Formatting the Data  

We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. 

For a bound column we add  DataFormatString="{0:N2}%" to the definition of the column.  Looking at the table below, we can see that in our example we shall format the column to have to digits after the decimal point , followed by a percentage sign.

For a template column we add the formatting into the reference to the data:
text='<%# DataBinder.Eval(Container, "DataItem.Total", "{0:c}") %>'

Expression Description
{0:C} Currency item based on the locale, For example £29.99
{0:D4} A number of the set number plus 1.  For example 00124
{0:N2}% A number with two decimal places, followed by a percentage sign
{0:0.0} A number rounded to one decimal place.
{0:D} The date represented as a long string.  For example Monday, 20 July 2004
{0:dd-MM-yy} The number as defined by the day, month and year code.  for example 20-07-04

To illustrate the above I have included below an example template column.  As can be seen two different formatting styles are used. When the datagrid is normally viewed  the currency format is displayed and when the row is being edited we switch to a 2 decimal place view, to provide the number in a suitable format, without the presentation of the currency symbol.

<HEADERSTYLE HorizontalAlign="Center"></HEADERSTYLE>
<ITEMTEMPLATE>
    <asp:Label Text='<%# DataBinder.Eval(Container, "DataItem.price", "{0:c}") %>' runat="server">
</asp:Label>
</ITEMTEMPLATE>
<EDITITEMTEMPLATE>
    <asp:TextBox id=price Text='<%# DataBinder.Eval(Container, "DataItem.price", "{0:N2}") %>' runat="server" Columns="6">
    </asp:TextBox>
</EDITITEMTEMPLATE>


Copyright 2005-2015, Megasolutions Ltd  |   Privacy Policy