0

Apologies for the newb question, but I'm having a hard time wrapping my head around how to access a list of ints within a generic list and then displaying all of that information into a listview (winform). Here is what I have so far...

    }
    List<Student> students;
    public class Student
    {

        public string First { get; set; }

        public string Last { get; set; }

        public List<int> Scores { get; set; }


        public int ID { get; set; }


        public string[] ToListViewItem()
        {
            return new string[] {
                First,
                Last,
                ID.ToString(),
                Scores.ToString()
            };

        }

    }

    protected override void OnLoad(EventArgs e)

    {
        base.OnLoad(e);

        students = new List<Student>


        {

        new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},

        new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},

        new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},

        new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},

        new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},

        new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},

        new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},

        new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},

        new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},

        new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},

        new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},

        new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }


    };

        PopulateListView(students);


        GetAverageGrade();

    }

    public void GetAverageGrade()
    {
        foreach (Student s in students)
        {
            double avg = s.Scores.Average();


        }

    }

    public void PopulateListView(List<Student> list)
    {

        listView1.SuspendLayout();

        for (int i = 0; i < list.Count; i++)
        {

            // create a list view item
            var lvi = new ListViewItem(list[i].ToListViewItem());
            // assign class reference to lvi Tag for later use
            lvi.Tag = list[i];
            // add to list view
            listView1.Items.Add(lvi);

            //This adjust the width of 1st column to fit data.
            listView1.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.ResumeLayout();
        }


    }
}

So far I'm able to get the first three columns of my listview to populate with the names and student ids, but all I'm getting in that final column is "Systems.Collections.Generic...",etc.

Any help would be greatly appreciated, thanks.

DB101er
  • 11
  • 3

2 Answers2

1

You're probably using the Scores.ToString() method which returns the classname. You might try: string.Join(", ", Scores) instead. This will concat all values separated with a ,.

public string[] ToListViewItem()
{
    return new string[] {
        First,
        Last,
        ID.ToString(),
        string.Join(", ", Scores)
    };
}
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

That's what Scores.ToString() returns.

If you want to display useful information, you need to write code that consumes your List<int> and returns useful information.

Start by looking at string.Join().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964