Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


c# Reading data from CSV file into a List Object and display in ListView in Windows Form

I need help reading data from a CSV file into a List Object which will be displayed in ListView in Windows Form. I have two classes created Employee(Superclass) and Developer(subclass) inherits from Employee. Here is the code: // Employee Class lass Employee { public string Name { get; set; } public string Address { get; set; } public int Age { get; set; } public double GrossMonthlyPay { get; set; } public int DepartmentID { get; set; }

    public Employee() { }

// Developer Class class Developer:Employee

    public string DevType { get; set; }
    public string EmpTaxType { get; set; }
    public double AnnualTax { get; set; }
    public double AnnualNetPay { get; set; }

    public Developer() { }

// The part I need help with is upon clicking the button, it should read data from file into a list and display on ListView Control

private void button1_Click(object sender, EventArgs e) { // Reads and store data from text file into List string[] lines;

        lines = File.ReadAllLines(@"Employees.csv");

        foreach (var line in lines)


            string[] words = line.Split(',');

            List <Developer> Developer = new List<Developer>(line.Length);

            foreach (var word in words)

                List<Developer> developerList= new List<Developer>();
                Developer Developer = new Developer();

                // Stuck here how to proceed as I tried to assign it I get paragraphs of errors and exceptions
                Developer.name = words[0];
                Developer.address = words[1];
                Developer.age = Convert.ToInt32(words[2]);
                Developer.grossMonthlyPay = Convert.ToDouble(words[3]);
                Developer.departmentID = Convert.ToInt32(words[4]);
                Developer.devType = words[5];
                Developer.empTaxType = words[6];
                Developer.annualTax = Convert.ToDouble(words[7]);
                Developer.annualNetPay = Convert.ToDouble(words[8]);

                int count = 0;

                devArray[count] = Developer;
                foreach (var Developers in devArray)
                    ListViewItem item = new ListViewItem(Developer.name);
                    item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.address));
                    //item.SubItems.Add (new ListViewItem.ListViewSubItem (item, Developer.Age));
                     //item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.GrossMonthlyPay));
                    //item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.DepartmentID));
                    item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.devType));
                    item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.empTaxType));
                    // item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.AnnualTax));
                    // item.SubItems.Add(new ListViewItem.ListViewSubItem(item, Developer.AnnualNetPay));


I was trying to read data into an Array Object but no longer needs that and I would need to manipulate the data and it is easier to do via List

1 Answer

Steven Parker
Steven Parker
221,450 Points

At first glance, it looks like your errors might mostly be due to inconsistent capitalization in the field names. For example, in the definition for "Employee", you have a property "Name" (capital "N"), but in the code you assign "Developer.name" (lower-case "n").

Also, when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Thanks, I did change it from lower case to upper case but it does not fix the problem Sorry about the code format and thanks for tip.

Steven Parker
Steven Parker
221,450 Points

You can always post the revised code (with formatting) if you still need help with it.