0

I have a List<Student> where for every student 4 properties.

At the moment I'm using this:

listStudents = new List<Student>();

foreach (Student s in listStudents)
{
   listbox1.Items.Add(s);
}

But it shows the 4 properties next to each other.

I did some research to sort the properties in columns, and found listview.

Can anyone explain me how can i do this?

I tried to add columns to the collection of the listview, but it still didn't work...

I tried also it:

listStudents = new List<Student>();

foreach (Student s in listStudents)
{
   listview.Items.Add(s);
}

Can anyone tell me what I'm doing wrong? I just want the 4 propertys for each student in different columns.

2
  • see this stackoverflow.com/questions/9951704/… Commented Jun 19, 2013 at 4:23
  • I actually like listview instead of listbox. It can look fancier with not much fancy code. It can look like a grid. Add headers/columns to it, then add items. I think, you need to add each particular column value separately. Commented Jun 19, 2013 at 4:32

2 Answers 2

0

First set the column names (you can do this with the graphical designer):

listView1.Columns.Add("col 1");
listView1.Columns.Add("col 2");
listView1.Columns.Add("col 3");
listView1.Columns.Add("col 4");

and then add rows (I assume here that your properties are strings):

foreach(Student s in listStudents){
    listView1.Items.Add(new string[]{
        s.Property1, s.Property2, s.Property3, s.Property4
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0
            List<Student> StudentsList = new List<Student>();
            Student StuObj = new Student();
            StuObj.ID = 1;
            StuObj.age = 23;
            StuObj.name = "ROCK";
            StudentsList.Add(StuObj);
            foreach (Student s in StudentsList)
            {
                string[] array = { s.ID.ToString(), s.age.ToString(), s.name.ToString()};
                var items = listView1.Items;
                foreach (var value in array)
                {
                    items.Add(value);
                }

            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.