Link to home
Start Free TrialLog in
Avatar of karakav
karakav

asked on

ASP.NET Currency format string in a GridView

Can someone explain me how I can format currency data in a GridView? I tried Microsoft way but it doesn't work.
<asp:GridView ID="GridView1" runat="server">
    <Columns>                                                
	<asp:TemplateField>
	    <HeaderTemplate>
		<asp:LinkButton ID="LinkButton1" runat="server" Text="Total" CommandName="Sort" CommandArgument="Total"/>
	    </HeaderTemplate>
	    <ItemTemplate>
		<asp:Label ID="Label1" runat="server" Text='<%#Eval("Total","{0:c}") %>' />
	    </ItemTemplate>
	</asp:TemplateField>
    </Columns>
</asp:GridView>

Open in new window

Avatar of satheeshm
satheeshm

you can use format currency function            
<asp:Label ID="Label1" runat="server" Text='<%#formatcurrency(Eval("Total"),2) %>' />
Avatar of karakav

ASKER

And where do I get the formatcurrency reference?
sorry...formatcurrency is exclusive to vb.net. your's is C#...in C#. you can do this way

<%# DataBinder.Eval( Container.DataItem, "NAME OF THE FIELD").ToString("{0:c}") %>

for further assistance you may see the below link (old question in EE)

https://www.experts-exchange.com/questions/22707634/Vb-Net-to-C-conversion-cont.html
Avatar of karakav

ASKER

This doesn't work neither. Even if it did, it think I would get an error in case the field didn't contain any value.
Avatar of karakav

ASKER

By the way, why this problem doesn't affect datetime values?
In which currency you wish to display your result?
try this?

 Text="<%# Eval('Total','{0:###,###,###0.#0}') %>"
Avatar of karakav

ASKER

No, it is not working.
Avatar of karakav

ASKER

I actually found my own solution but I am not confortable with it. It is so long.
<%#(Convert.ToDecimal(DataBinder.Eval(Container.DataItem,"Total"))).ToString("C2")%>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nikege
nikege

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Look at these articles for more information about Formating and String.Format method
http://msdn.microsoft.com/en-us/library/txafckwd.aspx - 'Composit Formating'
and
http://msdn.microsoft.com/en-us/library/fbxft59x.aspx - 'Formating Types'
Avatar of karakav

ASKER

Thanks. Your solution is more pratic.