Mansel Mansel - 1 year ago 79
ASP.NET (C#) Question

ASP:Chart number localised formatting without changing culture

I have an ASP:Chart control in a web application (ASP.Net). They are used to display some basic accounting information, like so:

Lovely chart

However, I want to be able to change the decimal separator to a comma, for display in other countries that used the comma, e.g. -28606.62 should display as -28606,62.

I know I can change the current culture in the code behind for the page, and this will format the numbers correctly, however I am reluctant to do this as this will change the culture for the thread/page, and thus any calculations etc that may be done during the life of the page may screw up.

Is there another way of specifying the decimal separator on the ASP:Chart control?

I've tried playing with the LabelStyle.Format property, but this doesn't allow you to change the separators, only the format of the number.

Many thanks

Answer Source

You should use the FormatNumber event on the chart control as shown below:

yourChart.FormatNumber += Chart_FormatLocalisedNumber;

And your event code:

     private void Chart_FormatLocalisedNumber(object sender, FormatNumberEventArgs e)
            e.LocalizedValue = e.Value.ToString("N2", YourFormatProvider);

Inside the event you can set the e.LocalizedValue to what ever you want and this will be what is displayed on the chart.

In the example above i used a Standard Numeric Format String and passed in a format provider (such as new Culture("nl-NL")) which has the "," as a decimal symbol.