Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialnirmalabasdeo
Python Development Techdegree Student 3,787 Pointsc# 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));
listView1.Items.Add(item);
}
}
}
}
1 Answer
Steven Parker
231,248 PointsAt 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. Or watch this video on code formatting.
nirmalabasdeo
Python Development Techdegree Student 3,787 PointsThanks, 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
231,248 PointsYou can always post the revised code (with formatting) if you still need help with it.
nirmalabasdeo
Python Development Techdegree Student 3,787 Pointsnirmalabasdeo
Python Development Techdegree Student 3,787 PointsI 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