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

endrec
endrec used Ask the Experts™
on
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>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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>



Author

Commented:
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).
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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

Author

Commented:
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;
Tom KnowltonWeb developer

Commented:
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.
I believe I have provided a lot of possible solutions/recommendations in this thread.  ID:35775133, ID:35775168 (modify culture programmatically), and ID:35775196 (ItemDataBound event, thread level culture change) supports the requirements of the asker ID:35775177.  However, I provided a higher level approach rather than using ItemDataBound which is suggested in ID:35775229.    I don't know why the asker didn't came back for feedback but I believe I covered his requirement.

protected void Page_Load(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ja-JP");

            //The thread level culture has been changed to Japanese which will show currency format from $ to ¥ after the DataBind.
            DataGrid1.DataBind();

            //Return to original culture

            System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
        }

I believe it would be unfair to just delete this question as I covered or answered what is required by the asker.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial