Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have written a code to read the csv file and add it to the grid of ListView in WPF. Created all the columns statically in the XAML file and mapped to the respective columns. But i would like to create grid based on the header from the CSV file. As the input csv header is not constant in my application. So is there any way i can create the grid dynamically and update the data in the WPF screen.

What I have tried:

Created all the columns statically in the XAML file and mapped to the respective columns
Posted
Updated 10-Feb-23 6:48am
Comments
Andre Oosthuizen 10-Feb-23 2:53am    
This might help - https://stackoverflow.com/questions/68117954/dynamically-create-property-based-on-if-csv-has-corresponding-header-using-csv-h
Rocky_Bas 10-Feb-23 3:04am    
Even in this link its creating two different classes for different format of headers. But i want to create the grid(that i generally do in XAML)dynamically from the code based on the input from the CSV file. Want to keep it dynamic so that i dont need to change the code later if the headers are changed
Graeme_Grant 10-Feb-23 3:39am    
What solution have you come up with is better than that in the link?

I have used CsvHelper[^] to parse the CSV file. CSV files are a little tricky and must confirm to the RFC 4180: Common Format and MIME Type for Comma-Separated Values (CSV) Files[^].

Now, using the CsvHelper, here is one way to dynamically load a WPF Listview creating headers on-the-fly and dynamically loading the data:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        LoadData();
    }

    List<string[]> items = new();
    private void LoadData()
    {
        MyListView.View = new GridView();
        GridView grid = (MyListView.View as GridView)!;

        using (var reader = new StreamReader("book1.csv"))
        using (var csv = new CsvReader(reader,
                                       CultureInfo.InvariantCulture))
        {
            csv.Read();
            csv.ReadHeader();
            for (var index = 0; index < csv.HeaderRecord!.Length; index++)
            {
                var value = csv.HeaderRecord[index];
                grid.Columns.Add(new GridViewColumn()
                {
                    Header = csv.HeaderRecord[index],
                    DisplayMemberBinding = new Binding($"[{index}]")
                });

            }


            while (csv.Read())
            {
                int count = csv.Parser.Record!.Length;
                string[] item = new string[count];

                for (int index = 0; index < count; index++)
                {
                    item[index] = csv.Parser.Record[index];
                }

                items.Add(item);
            }
        }

        MyListView.ItemsSource = items;
    }
}
 
Share this answer
 
Comments
Rocky_Bas 12-Feb-23 22:25pm    
This works fine but have a concern here is there any way we can skip first two lines while reading the csv as the header & content starts from 3rd line. First two line consists of some version and date time in CSV so i want to skip them
Graeme_Grant 12-Feb-23 22:34pm    
Add another csv.Read(); to skip over any unwanted rows... so:
csv.Read(); // skip a row...
csv.Read(); // skip a row...

csv.Read();
csv.ReadHeader();
for (var index = 0; index < csv.HeaderRecord!.Length; index++)
{
	var value = csv.HeaderRecord[index];
	grid.Columns.Add(new GridViewColumn()
	{
		Header = csv.HeaderRecord[index],
		DisplayMemberBinding = new Binding($"[{index}]")
	});

}

while (csv.Read())
{
 // do work here...
}

csv.Read(); reads from the stream into a buffer, csv.ReadHeader(); and csv.Parser.Record decodes the buffer - first for the header, second for the data.
Rocky_Bas 12-Feb-23 22:46pm    
Thanks it worked
Graeme_Grant 12-Feb-23 22:48pm    
Great.
 
Share this answer
 
Comments
Graeme_Grant 10-Feb-23 15:39pm    
How is this any way related to the question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900