简体   繁体   中英

How to get a WPF datagrid to display characters correctly?

I have a simple datagrid that I'm binding to a CSV file, but the data is being displayed incorrectly as follows.

在此处输入图片说明

How can I change it so the '?' characters are displayed as ' ' (space) characters as they're supposed to be?

The grid is very simply defined as:

<Grid>
    <DataGrid AutoGenerateColumns="True" FontFamily="Lucida Sans Unicode"
          ItemsSource="{Binding}">
    </DataGrid>
</Grid>

And I populate the grid as follows:

    public MainWindow()
    {
        InitializeComponent();

        string startupPath = System.IO.Directory.GetCurrentDirectory();
        DataContext = TranslationService.ReadFile(Path.Combine(startupPath, "translations.csv"));
    }

public static class TranslationService
{
    public static List<TagEntry> ReadFile(string filepath)
    {
        var lines = File.ReadAllLines(filepath);

        var data = from l in lines.Skip(1)
                   let split = l.Split(',')
                   select new TagEntry
                   {
                       Tag = split[0],
                       English = split[1],
                       Irish = split[2],
                   };

        return data.ToList();
    }
}

Have you try to force the encoding ?

Use :

File.ReadAllLines(filepath, Encoding.UTF8);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM