3

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 DataGrid in an WPF project. I cannot, for the life of me, get it to import. What I was trying to do before was add all the columns (Header1, Header2, Header3, Header4) then add the rows... but there didn't seem to be any way to add rows. So I tried using ItemSource... but no luck.

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

UPDATE

So I tried this:

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

And it seems to work... somewhat: The Rows show up, but no columns or content

UPDATE 2

So after turning on AutoGenerateColumns, everything worked perfectly.

2 Answers 2

6

Take a look at this library. It lets you convert any CSV into an object of type DataTable and bind that to the DataGrid like this:

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

If you already have parsed your CSV into a table, just bind the ItemSource to the table's DefaultView property and set AutoGenerateColums to true

Sign up to request clarification or add additional context in comments.

5 Comments

Well this is just sample data... I want it so it can load and display any CSV file.
I can already convert CSV to DataTable... but it causes an error when I set that as the ItemsSource for my DataGrid
You should be able to bind to table.DefaultView which is of type DataView. See above
I tried this, and it seemed to work somewhat... see updated question.
Try setting dataGrid.AutoGenerateColumns = true
0
public partial class MainWindow : Window
{
    public ObservableCollection<CsvData> CsvDataList { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        CsvDataList = new ObservableCollection<CsvData>();
        dataGrid.ItemsSource = CsvDataList;
        LoadCsvFile();
    }

    private void LoadCsvFile()
    {
        OpenFileDialog openFileDialog = new OpenFileDialog
        {
            Filter = "CSV files (*.csv)|*.csv"
        };
        if (openFileDialog.ShowDialog() == true)
        {
            using (StreamReader reader = new StreamReader(openFileDialog.FileName))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    var values = line.Split(','); // Assuming a comma is the delimiter

                    // Map CSV data to the CsvData class
                    CsvDataList.Add(new CsvData
                    {
                        Column1 = values[0],
                        Column2 = values[1],
                        Column3 = values[2],
                        Column4 = values[3],
                        Column5 = values[4]
                    });
                }
            }
        }
    }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.