2013-06-19 42 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); 
       } 

      } 
相關問題