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>
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
<%# 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
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.
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}') %>"
Text="<%# Eval('Total','{0:###,###,#
ASKER
No, it is not working.
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")%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'
http://msdn.microsoft.com/en-us/library/txafckwd.aspx - 'Composit Formating'
and
http://msdn.microsoft.com/en-us/library/fbxft59x.aspx - 'Formating Types'
ASKER
Thanks. Your solution is more pratic.
<asp:Label ID="Label1" runat="server" Text='<%#formatcurrency(Ev