Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5104
  • Last Modified:

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

0
karakav
Asked:
karakav
  • 6
  • 2
  • 2
  • +2
1 Solution
 
satheeshmCommented:
you can use format currency function            
<asp:Label ID="Label1" runat="server" Text='<%#formatcurrency(Eval("Total"),2) %>' />
0
 
karakavAuthor Commented:
And where do I get the formatcurrency reference?
0
 
satheeshmCommented:
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)

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22707634.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
karakavAuthor Commented:
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.
0
 
karakavAuthor Commented:
By the way, why this problem doesn't affect datetime values?
0
 
meispiscesCommented:
In which currency you wish to display your result?
0
 
KrummelzCommented:
try this?

 Text="<%# Eval('Total','{0:###,###,###0.#0}') %>"
0
 
karakavAuthor Commented:
No, it is not working.
0
 
karakavAuthor Commented:
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

0
 
nikegeCommented:
You can try these options

1. <%# ((Decimal)Eval("Total")).ToString("C2") %> - if you know that Total is not a string
2. <%# Convert.ToDecimal(Eval("Total")).ToString("C2") %> - if total might be a string
3. <%# string.Format("{0:C2}", Eval("Total")) %>
0
 
nikegeCommented:
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'
0
 
karakavAuthor Commented:
Thanks. Your solution is more pratic.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now