首页 > 解决方案 > Displaying Extended Characters importing CSV to DataGridView

问题描述

I'm using Visual Studio 2019 and making a Desktop Application using C#.

Basically, I'm trying import a CSV file into DataGridView. Everything works when I run the program, except that there are special characters that show up with ? blocks. The file in question uses the Windows-1252 (Western European) character map. It's a game file (the program is intended to help me mod the game more efficiently) so I can't just change the encoding of the file.

I'm using a csvParser solution that I found here on StackOverflow - I've posted the code that sends the data to the table after a button click.

As you can see, I tried adding "Encoding.UTF8" to TextFieldParser but it doesn't help.

I've also tried changing the font used in the table itself, but I tried a couple of Unicode fonts and none of them helped (currently using Segoe UI).

Hope someone can help!

        {
            string path = "C:\\Users\\user\\Documents\\Paradox Interactive\\Europa Universalis IV\\mod\\testmod\\map";
            openFileDialog1.InitialDirectory = path;
            openFileDialog1.Title = "Open Definitions File";
            openFileDialog1.ShowDialog();

            path = openFileDialog1.FileName;

            using (TextFieldParser csvParser = new TextFieldParser(path, Encoding.UTF8))
            {
                csvParser.CommentTokens = new string[] { "#" };
                csvParser.SetDelimiters(new string[] { ";" });
                csvParser.HasFieldsEnclosedInQuotes = false;

                csvParser.ReadLine();

                while (!csvParser.EndOfData)
                {
                    string[] fields = csvParser.ReadFields();
                    string defID = fields[0];
                    string defRed = fields[1];
                    string defGreen = fields[2];
                    string defBlue = fields[3];
                    string defProvince = fields[4];
                    string defBlank = fields[5];

                    string[] row0 = { defID, defProvince, defRed, defGreen, defBlue, defBlank };

                    defData.Rows.Add(row0);
                }
            }
        }

标签: c#csvdatagridview

解决方案


推荐阅读