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
Solved

ASP.NET Currency format string in a GridView

Posted on 2009-07-06
12
4,678 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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