We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Changed regional setting - color format throws exception

Hi,

I am developing an app that needs to be culture independent.

I have a cell with this format:

"TextColor = 255, 255, 255
Font.Size = 12
Interior = Solid; 0, 0, 255"

The format is set to a cell by passing the format(as a string) to cell's GridStyleInfo.ParseString(format). It works ok if select en-US, en-UK, en-AU or sw-KE. However, if i changed the regional setting to ms-MY (Malay-Malaysia), sv-FI and nl-BE it throws exception saying

"255, 255, 255 is not a valid value for Int32".

Is this have something to do with the decimal symbol? Any idea for workaround on this issue?

Thanks,
Jamilah

RegionalTest.zip

4 Replies

HA haneefm Syncfusion Team July 11, 2007 09:44 PM UTC

Hi Jamilah,

One way you can do this by handling the Parse and Format events on the GridStyleInfoProperty class to work around this problem. The reason is that if you save a object type out, the grid does not know how to parse it. So, when CellValue is serialized, you also have to include a type so the serialization knows what to do with the object. Here is a little sample with a button handler that mimics what you have.

GC_FormulaParse.zip

Best regards,
Haneef


JA Jamilah Abdul Rashid July 17, 2007 11:07 AM UTC

Hi again,

I think i didn't explain clear enough about the problem.

The issue is, if I have a cell and the CellValueType/CellValue can be any type (int/string), how can i set the format for the TextColor/BackColor? The format that I'm using is - "TextColor = 255, 255, 255".

What I'm currently using is get the cell's GridStyleInfo object and call "ParseString" and send in the format as a string(i.e pass in "TextColor = 255, 255, 255".

It works using that way, however, if I changed the current CultureInfo and refresh the grid, the ParseString function will throw exception saying
"255, 255, 255 is not a valid value for Int32". Why is this happening?

Here are the sample code:

//set the default culture info
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

//create a default format string
GridStyleInfo style = new GridStyleInfo();
style.TextColor = Color.FromArgb(255, 255, 255);
string format = style.ToString(); // format value now is "TextColor = 255, 255, 255"

//apply the format to the GridStyleInfo
GridStyleInfo newStyle = new GridStyleInfo();
newStyle.ParseString(format);
string newFormat = newStyle.ToString(); // newFormat value now is "TextColor = White"

//now change the CultureInfo
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ms-MY");
//use the default format again
newStyle.ParseString(format); // <-- this is where it throws exception
string newfred2 = newStyle.ToString();


I also check the InnerException that says:

{System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)}

How can I fix this. Hope you can help.

Thanks,
Jamilah


HA haneefm Syncfusion Team July 17, 2007 08:52 PM UTC

Hi Jamilah,

Below is a sample that shows you "How to convert the string from style object in a grid?". Please try this and let me know if this helps.
ModifiedRegionalTest.zip

[c#]
public static Color CurrentParseColor;

public static string ParseString( string s)
{
int index = s.IndexOf("TextColor");
if (index != -1)
{
int iend = s.IndexOf('',index);
string textColor = s.Substring(index, index + iend+1);
int iequalSign = textColor.IndexOf('=');
string colorstring = textColor.Substring(iequalSign + 1).Replace("\r", "").Replace("","") ;

TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color));
object obj = converter.ConvertFromInvariantString(colorstring);
CurrentParseColor = (Color)obj;
s = s.Replace(textColor, "");
}
return s;
}

Best regards,
Haneef


JA Jamilah Abdul Rashid July 18, 2007 12:33 AM UTC

Hi Haneef,

Thanks, this solved my problem. I really appreciate it.

Thanks again.

Cheers,
Jamilah

Loader.
Live Chat Icon For mobile
Up arrow icon