Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi ,
Trying to add United States Dollar Symbol $ to a columns cell, but it returns the Euro symbol ?
BACKGROUND:
The program im building is currency converter where a user enters an amount in a numericUpdown and a combobox with all the worlds currencys. The amount entered is then multiplied by the value in the column cells. The new Value is then displayed in those cells with the correct symbol.

Note: i can add the $ doing this;
C#
node.Cells[2].Value = "$ " + numVal * usa;

But i would like to use CultureInfo

What I have tried:

C#
if (toolStripComboBox1.SelectedIndex >= 0)
{
   foreach (TreeGridNode node in myTGV.Rows)
    {
        
                       
        if (node.Cells[2].Value != null)
        {
            myTGV.Columns["Cost"].DefaultCellStyle.Format = String.Format("C", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
            //string value = node.ToString();
            //node.Cells[2].InheritedStyle.Format = "#,###.##";

            int numVal = Convert.ToInt32(node.Cells[2].Value);

            float usa = (float)numCurrency.Value;
            //string value = numVal * usa;
            node.Cells[2].Value = numVal * usa;
        
        }



    }
                
}
Posted
Updated 21-Jun-16 7:57am
Comments
Richard Deeming 21-Jun-16 11:51am    
What is the full type name of the TreeGridNode class?

You're setting the DefaultCellStyle.Format property to the literal string "C". The String.Format call doesn't do anything, since you don't have any format placeholders within the string.

You need to find a way to tell the node, the control, the form, or the entire application to use a specific culture to format values.
BEBE2011 27-Jun-16 12:34pm    
Solution 2 has resolved this error.

To be able to properly set DataGridViewCell format, you have to set 2 properties:
1) DataGridViewCellStyle.Format = "C";
2) DataGridViewCellStyle.FormatProvider = IFormatProvider; //default is CurrentUICulture

For more details, please see:
How to: Format Data in the Windows Forms DataGridView Control[^]
DataGridViewCellStyle.FormatProvider Property (System.Windows.Forms)[^]
 
Share this answer
 
Comments
BEBE2011 23-Jun-16 4:39am    
HI , Thanks for the reply can you give me an example of how to use it using my code.
Maciej Los 23-Jun-16 5:11am    
I did already ;)
In addition to using a culture directly for formatting, you can assign a culture to the whole thread. Please see:
Thread.CurrentCulture Property (System.Threading),
Thread.CurrentUICulture Property (System.Threading).

You did not explain the problem with your code sample, but I can see one somewhat unrelated problem: you create an instance of System.Globalization.CultureInfo again and again, in your loop. Why? You should create this object once and reuse it.

—SA
 
Share this answer
 
Comments
Richard Deeming 21-Jun-16 11:52am    
Worse than that - the created CultureInfo isn't even required, as the first argument to String.Format doesn't contain any placeholders to format.

The whole line could be replaced with:
myTGV.Columns["Cost"].DefaultCellStyle.Format = "C";
Sergey Alexandrovich Kryukov 21-Jun-16 11:57am    
Agree. Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900