Solved

ASP.NET Currency format string in a GridView

Posted on 2009-07-06
12
4,529 Views
Last Modified: 2013-12-17
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
Comment
Question by:karakav
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 5

Expert Comment

by:satheeshm
ID: 24784062
you can use format currency function            
<asp:Label ID="Label1" runat="server" Text='<%#formatcurrency(Eval("Total"),2) %>' />
0
 
LVL 4

Author Comment

by:karakav
ID: 24784078
And where do I get the formatcurrency reference?
0
 
LVL 5

Expert Comment

by:satheeshm
ID: 24784113
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
 
LVL 4

Author Comment

by:karakav
ID: 24784180
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
 
LVL 4

Author Comment

by:karakav
ID: 24784187
By the way, why this problem doesn't affect datetime values?
0
 
LVL 7

Expert Comment

by:meispisces
ID: 24784546
In which currency you wish to display your result?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Expert Comment

by:Krummelz
ID: 24784707
try this?

 Text="<%# Eval('Total','{0:###,###,###0.#0}') %>"
0
 
LVL 4

Author Comment

by:karakav
ID: 24784870
No, it is not working.
0
 
LVL 4

Author Comment

by:karakav
ID: 24784877
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
 
LVL 2

Accepted Solution

by:
nikege earned 500 total points
ID: 24793367
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
 
LVL 2

Expert Comment

by:nikege
ID: 24793399
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
 
LVL 4

Author Closing Comment

by:karakav
ID: 31600105
Thanks. Your solution is more pratic.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now