Home > Net >  how to show my csv file and edit it in gridview WPF
how to show my csv file and edit it in gridview WPF

Time:04-01

I have a simple csv reader that I wrote, it receives data in the format List<string[]> . I need to show this data in wpf and edit it. I use mvvm. If I write just like that, it won't do anything.

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Table}"> </DataGrid>

How can I do this? Is it possible for me to somehow output and edit data in a convenient format in the form of List<string[]>. A csv table can have an arbitrary number of columns, so I can't make any particular class inside the code. I tried to find an answer to this question on the Internet, looked at several similar projects, but something never figured it out.

Let's say I get the data this way

var _fileName = @"F:\test.csv";

CsvWriter reader = new CsvWriter(_fileName);

foreach(var item in reader.Read())
{
     Table.Add(item);
}

CodePudding user response:

There is a library called CSVReader. It converts any csv files to DataTables

Example:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OpenCSVButton_Click(object sender, RoutedEventArgs e)
        {   
            DataTable dt = LoadCSVFileFromPath();
            dataGrid.ItemsSource = dt.DefaultView;

            //Simple method to convert DataTable to a List of strings
            AddToList(dt);
        }

        private List<string> AddToList(DataTable table)
        {
            List<string> strTable = new List<string>();
            foreach(DataRow rows in table.Rows)
            {
                foreach (DataColumn cols in table.Columns)
                    strTable.Add(rows[cols].ToString());
            }

            return strTable;
        }

        private DataTable LoadCSVFileFromPath()
        {
            //Use this method to convert your CSV file to DataTable
            DataTable table = CSVReader.ReadCSVFile("FILEPATHGOESHERE", true);
            return table;
        }
    }

You can play around and achieve this in MVVM by binding the DefaultView of the DataTable table to DataGrid

CodePudding user response:

Dont use Binding.

XAML:

<DataGrid x:Name="dataGrid1" AutoGenerateColumns="True" />

Read the CSV data and conver it to DataTable, and place it into ItemsSource:

public partial class MainWindow : Window
{ 
    public MainWindow()
    {
        InitializeComponent();
        LoadData();
    }

    DataTable GetDataTable(CsvReader csv)
    {
        var dt = new DataTable();

        csv.Read();
        csv.ReadHeader();
        foreach (var header in csv.HeaderRecord)
            dt.Columns.Add(header);

        var cols = dt.Columns.Count;
        while (csv.Read())
        {
            var row = dt.NewRow();

            for (int i = 0; i < cols; i  )
                row[i] = csv[i];

            dt.Rows.Add(row);
        }

        return dt;
    }

    DataTable table;
    string _fileName = @"F:\test.csv";
    void LoadData()
    {
        using (var reader = new StreamReader(_fileName ))
        using (var csvReader = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = "," }))
        {
            table = GetDataTable(csvReader);
            table.AcceptChanges();
            dataGrid1.ItemsSource = table.AsDataView();
        }
    }

    void SaveData()
    {
        if (table.GetChanges() != null)
        {
            //write DataTable to csv...
        }
    }

}
  • Related