0

My issue is displaying european characters like 'ä' and 'ö' in WPF datagrid. Data is loaded from .csv. I'm using .net framework 4.0.

Filepath includes the .csv characters at the end. CSV file is generated and opened by the following code.

    public static void GenerateCsvTemplate(bool overwrite)
    {
        try
        {
            if (overwrite)
            {
                if (!File.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }

                string delimiter = Settings.Default.SplitValue.ToString();
                string content = "a;b;c;d;f";
                File.WriteAllText(filePath, content,UTF8Encoding.UTF8);
            }
            Process.Start(filePath);
        }
        catch (Exception ex)
        {
            logger.Error(ex.Message);
        }
   }

Creating the file in UTF-8 encoding is not changing the output of my european characters on my datagrid. According to my understanding the default encoding should be UTF-8 in WPF. Users modify the template and save it in .csv in excel. When they enter european characters they are not displayed correctly in datagrid which reads the .csv file.

Below the code when I'm reading the .csv file:

using (var reader = new StreamReader(File.OpenRead(FileHelper.filePath), UTF8Encoding.UTF8)) 
{
     //Read file and populate objects
}

I think I'm missing something.

Jermay
  • 97
  • 8
  • Excel (2007 and more) require a BOM header for UTF-8 files. Otherwise it treats files as ANSI (likely ISO-8859-1). – Raphaël Feb 19 '13 at 12:28
  • strings in .net are utf-16 so it depends on how you are moving the utf-8 data into that string before binding it to the datagrid? Show some "read file and populate objects" code and I am sure someone will see your mistake. – John Sobolewski Feb 19 '13 at 12:31
  • @jsobo There is nothing special done before binding. I'm reading each line of the file and populating object data on each line. – Jermay Feb 19 '13 at 13:26
  • set break point when you have populated your object. does the text look correct in the locals window? – John Sobolewski Feb 20 '13 at 13:01
  • @jsobo European characters are already showing as "�" when reading from stream. This indicates that my problem is with .csv generation. – Jermay Feb 21 '13 at 07:49
  • you could open the file in visual studio or notepad++ both have encoding detection so that you could get an idea of what the encoding actually is. It is possible the file isn't UTF-8 encoded... There is an interesting discussion of this in the following stack question: http://stackoverflow.com/questions/90838/how-can-i-detect-the-encoding-codepage-of-a-text-file – John Sobolewski Feb 21 '13 at 13:02

0 Answers0