Thursday, March 8, 2012

Culture overriding custom format

Hi, I've been having a weird problem with a report I'm making. I've done douzan of em, and the problem only appeared in one. Let say I have a text field, and I give it a format property (for the sake of the example, imagine its a date, and I set it as yyyy/MM/dd. Same result if I would use an expression with the Format function).

Now, depending on culture, the slashes gets overrided. Everything else work (so if I put yyyy/MM, the format is recognised and the day won't show...but it will use dashes instead of slashes, for example, with us culture).

How do I get it to always respect my format? I have, like I said, douzans of other reports and it works fine, the format string gets applied exactly: I don't know what the hell I could have changed. All the culture settings are at default, like in the reports that do work... I've tried diff-ing the source, can't see anything weird, but I'm sure I'm just being blind.

Any ideas?

Thank you.

The DateSeparator is defined on the DateTimeFomatInfo objects and the slash is being "translated" to the current culture.

If there's not an option to have the slash in the format string not interpreted as a DateSeparator, the only thing I can think of is to add a .Replace(CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator, "/") to the end of your Format statement.

|||

Just found this stumbling around on something else. Try "yyyy'/'mm'/'dd" as your format string. Found ithere, so you can fuss at Microsoft if it doesn't work.

|||

Makes sense. Yeah, escaping the slashes (let say, yyyy\/MM\/dd ) worked too.

But then, what do I do with numeric formats? (since the date was just an example). #,##0.00
Works fine in most of my reports, but in that particular one, no matter what format I put, the number gets translated with the current culture. What mostly gets me, is that it works in all the reports! Just not -that- one...ugh.|||

Those separators are also defined in a culture-specific way. I'd recheck that one report and make sure that you haven't accidentally specified a culture or that you did specify a culture on the other reports.

|||

Yup, you're right... I'm baffled, I tested the other reports, and not only did they have cultures set, but the formats never worked (I had a formula that switched the Language setting depending on a parameter set by the user, and the culture I always ended up printing my report coincidently had the correct separators).

My apologies for misleading you, I know it can be annoying when you're trying to help someone and they give you the wrong info :)

At least now I know I can backslash escape the magical separators... That is quite awkward that it does that, especially since its a bit different from the behavior of .NET formatters... Oh well!

No comments:

Post a Comment