2013-06-19 36 views
0

我有一个List<Student>其中每个学生4个属性。Listview c#而不是列表框

目前我使用的是这样的:

listStudents = new List<Student>(); 

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

但它显示了4个属性彼此相邻。

我做了一些研究,以列中的属性进行排序,并找到listview

任何人都可以解释我如何做到这一点?

我试图add columnslistview的集合,但它仍然没有工作...

我也试了一下:

listStudents = new List<Student>(); 

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

谁能告诉我,我在做什么错误?我只想为不同栏目的每个学生提供4个房产。

+0

看到这个http://stackoverflow.com/questions/9951704/add-item-to-listview-control –

+0

我其实喜欢listview而不是listbox。它看起来很漂亮,没有太多花哨的代码。它可以看起来像一个网格。添加标题/列到它,然后添加项目。我想,你需要分别添加每个特定的列值。 –

回答

0

首先将列名(你可以用图形设计做到这一点):

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

,然后添加行(我这里假设你的属性是字符串):

foreach(Student s in listStudents){ 
    listView1.Items.Add(new string[]{ 
     s.Property1, s.Property2, s.Property3, s.Property4 
    }); 
} 
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); 
       } 

      } 
相关问题