c# - 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);
}
}
}
解决方案
推荐阅读
- python - Keras 随机模型
- javascript - 如何使用 SailsJS 和 MongoDB 在生产模式下迁移模型
- python - 如何在python中从表中插入一列
- sql - bool_or 和 bool_and 有什么区别
- python - 在 python 中使用 open cv 时出现重塑错误
- networking - GCP 虚拟机实例遭到入侵。如何保护它?
- android - 如何在 android YearMonth 中更改日期格式
- javascript - 无法访问 JSON 对象中的属性
- algorithm - 使用排列和组合遍历 N*N 矩阵的方法数
- c# - 通过c#向外部网站提交登录表单时出错