Solved

ASP.NET Currency format string in a GridView

Posted on 2009-07-06
12
4,636 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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!

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.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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