C # Load CSV into DataGrid

So I have a CSV file:

    Header1,Header2,Header3,Header4
    Data11,Data12,Data13,Data14
    Data21,Data22,Data23,Data24
    Data31,Data32,Data33,Data34
    Data41,Data42,Data43,Data44

and a DataGridin a WPF project. I cannot, for the life of me, force him to import. What I tried to do before, is to add all the columns ( Header1, Header2, Header3, Header4), and then add the line ... but it seems that there was no way to add the line. So I tried to use ItemSource... but no luck.

So ... how do I import a CSV file into System. Windows. Controls.DataGrid

UPDATE

So, I tried this:

    DataTable table = CSVReader.ReadCSVFile(fileName, true);
    dataGrid.ItemsSource = table.DefaultView;

And it works ... a few: The Rows show up, but no columns or content

UPDATE 2

So, after turning on, AutoGenerateColumnseverything worked perfectly.

+3
source share
1 answer

. CSV DataTable DataGrid :

DataTable table = CSVReader.ReadCSVFile(fileName, true);
myGridView.ItemSource = table.DefaultView;
myGridView.AutoGenerateColumns = true;

CSV , ItemSource table DefaultView AutoGenerateColums true

+6

Source: https://habr.com/ru/post/1796065/


All Articles