Link to home
Start Free TrialLog in
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>
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).
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;
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

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial