Reputation: 21
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);
}
}
}
Upvotes: 2
Views: 273
Reputation: 131
Just use Encoding.GetEncoding(1252)
as the second parameter of the constructor, i.e. replace:
using (TextFieldParser csvParser = new TextFieldParser(path, Encoding.UTF8))
with:
using (TextFieldParser csvParser = new TextFieldParser(path, Encoding.GetEncoding(1252))
Upvotes: 0