Solved

ASP.NET Currency format string in a GridView

Posted on 2009-07-06
12
4,722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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