Avatar of endrec
endrec

asked on 

How can I display a currency value that is different than the user's culture in a templated grid item?

I wanted to know if there is an event/method for templated ASP.NET datagrid items where I can specifically set the culture that values are rendered in.

Example:  The user is viewing the page in Japanese, but all prices need to be listed as US prices with the $ currency while all other resources need to still be in Japanese.  For a specific datagrid and templated column, how can I set the culture that a particular column's values are rendered under.

Currently code like the following would show a price of 30 as 30¥ instead of $30.

<asp:TemplateColumn HeaderText="Example">
  <ItemTemplate>
    <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price", "{0:C}") %>'></asp:Label>
  </ItemTemplate>
</asp:TemplateColumn>
.NET ProgrammingC#ASP.NET

Avatar of undefined
Last Comment
Alfred A.
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Try this:

<asp:TemplateColumn HeaderText="Example">
  <ItemTemplate>
    <asp:Label ID="PriceLabel" runat="server" Text='<%# string.Format(new System.Globalization.CultureInfo("en-US"),"{0:C}", Eval("Price") %>'></asp:Label>
  </ItemTemplate>
</asp:TemplateColumn>



Avatar of endrec
endrec

ASKER

Oh, I need to do the formatting in the code behind because the culture of the currency is coming from the property that is in the code behind (which in terms comes from the database and may not be directly bound to the datasource of the grid).
Avatar of Alfred A.
Alfred A.
Flag of Australia image

You could also do it this way:

//Change Current Culture

System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

//Do something.

//Return to original culture

System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Check this link below to find other ways to modify culture programmatically.   In my previous post, that approach is based on thread level.

http://support.microsoft.com/kb/306162
Avatar of endrec
endrec

ASKER

Which method of the datagrid would I set the culture when generating the content for the label as it must be different than the culture of the rest of the page.  This is the primary issue.
Avatar of Alfred A.
Alfred A.
Flag of Australia image

You could put the culture modification in the ItemDataBound event.  There are other events such as ItemCreated.  It really depends on what you are doing anyway.

http://msdn.microsoft.com/en-us/library/a83x3931.aspx
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Also, you could apply the culture modification, something like this.

//Change Current Culture

System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

//Do Something
ItemsGrid.DataSource= CreateDataSource();
ItemsGrid.DataBind();

//Return to original culture

System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo