Home > database >  listview only printing one line, instead of whole file?
listview only printing one line, instead of whole file?

Time:09-13

I'm working on project, where i have to read a .csv file, where each line of the csv file contains 14 fields. however, i can only manage to print the first line with listview. i simply cant get it to print more than one line and i have no clue why.

this is my code:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var path = @"C:\Universitet\Privat\testfil.csv";

        using (TextFieldParser csvParser = new TextFieldParser(path))
        {
            csvParser.CommentTokens = new string[] { "#" };
            csvParser.SetDelimiters(new string[] { ";"});
            csvParser.HasFieldsEnclosedInQuotes = true;
            

            //skipper kolloner
            csvParser.ReadLine();

            int i = 0;
            ListViewItem rows = new ListViewItem();
            while (!csvParser.EndOfData) {
                //læser linje og pointer bevaeger sig til næste linje 
                string[] fields = csvParser.ReadFields();
                rows.SubItems.Add(fields[0]);
                rows.SubItems.Add(fields[1]);
                rows.SubItems.Add(fields[2]);
                rows.SubItems.Add(fields[3]);
                rows.SubItems.Add(fields[4]);
                rows.SubItems.Add(fields[5]);
                rows.SubItems.Add(fields[6]);
                rows.SubItems.Add(fields[7]);
                rows.SubItems.Add(fields[8]);
                rows.SubItems.Add(fields[9]);
                rows.SubItems.Add(fields[10]);
                rows.SubItems.Add(fields[11]);
                rows.SubItems.Add(fields[12]);
                rows.SubItems.Add(fields[13]);
                
            }
            listView1.Items.Add(rows);
        }
    }

Furthermore, the first line will only show if i actually click on the empty space, instead of just showing on load as it is supposed to.

this is the output of the code: output of code

CodePudding user response:

Your line is declared before the loop, so it is edited each time.

Change to this :

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    var path = @"C:\Universitet\Privat\testfil.csv";

    using (TextFieldParser csvParser = new TextFieldParser(path))
    {
        csvParser.CommentTokens = new string[] { "#" };
        csvParser.SetDelimiters(new string[] { ";"});
        csvParser.HasFieldsEnclosedInQuotes = true;
        

        //skipper kolloner
        csvParser.ReadLine();
        
        while (!csvParser.EndOfData) {
            //læser linje og pointer bevaeger sig til næste linje 
            string[] fields = csvParser.ReadFields();

            ListViewItem row = new ListViewItem("A NEW LINE !");
            row.SubItems.Add(fields[0]);
            row.SubItems.Add(fields[1]);
            row.SubItems.Add(fields[2]);
            row.SubItems.Add(fields[3]);
            row.SubItems.Add(fields[4]);
            row.SubItems.Add(fields[5]);
            row.SubItems.Add(fields[6]);
            row.SubItems.Add(fields[7]);
            row.SubItems.Add(fields[8]);
            row.SubItems.Add(fields[9]);
            row.SubItems.Add(fields[10]);
            row.SubItems.Add(fields[11]);
            row.SubItems.Add(fields[12]);
            row.SubItems.Add(fields[13]);

           listView1.Items.Add(row);
            
        }
    }
}
  • Related