Tuesday, July 14, 2009

Formatting data in Gridview cell

This is an important thing u need when working with grid-view. Suppose u want to format the numerical data in currency form or date form then you have to do a little trick.
Suppose your column name is
"Amount" should be shown as currency (ex: $100)
"PaymentDate" should be shown as date (ex:7/12/2009), generally SQL 2005 will show it as (7/12/2009 12.00 AM) which is a bad format.

so lets check it out. Suppose we are using TemplatedField in GridView and as field we are using labels ..so the code like this.

<asp:GridView ID="TestGV" runat="server">
<Columns>
<asp:TemplatedField>
<ItemTemplate>
<asp:Label ID="CurrencyLabel" runat="server" Text='<%#String.Format("{0:c}",Eval("Amount"))%>' > </asp:Label>
</ItemTemplate>
</asp:TemplatedField>
<asp:TemplatedField>
<ItemTemplate>
<asp:Label ID="PaymentDateLabel" runat="server" Text='<%#String.Format("{0:d}",Eval("PaymentDate"))%>' > </asp:Label>
</ItemTemplate>
</asp:TemplatedField>
</Columns>
</asp:GridView>

So this is the required code for converting into the desired format of data.

thanks.
Masud (15.7.9)

No comments:

Post a Comment