2013-11-21 21 views
1

我試圖將我的自定義類的對象的列表用作ListBox的源。問題是代替字符串顯示爲我的ListBox中的項目,整個字符串被分解並且各個字符變成ListBox項目。 下面是我的類和複製到ListBox。我在另一個班上存儲一個列表。在C#中使用List <>作爲BindingSource

我在做什麼錯?或者,也許有一個更好的方式將ListBox綁定到列表<>?

清單1 - 複製

  BindingSource bds1 = new BindingSource(_newProspectiveCustomer.PhoneNumbers, "PhoneNumber"); 
     phonesListBox.BeginUpdate(); 
     phonesListBox.DataSource = bds1; 
     phonesListBox.EndUpdate(); 

清單2 - 類

​​
+0

'_newProspectiveCustomer.PhoneNumbers'類型是什麼?一個'清單'?那麼'PhoneNumber'的定義是什麼? –

+0

您的示例中大約90%的代碼無關緊要,並且@KingKing指出您錯過了重要部分。 http://sscce.org/ –

回答

0

假設列表是PCPhone直接設置列表到listBox1中列表只是

BindingSource bds1 = new BindingSource() { DataSource = list }; 
listBox1.DisplayMember = "PhoneNumber"; 
listBox1.ValueMember = ... ; // value member 
listBox1.DataSource = bds1; 

或只是數據源

listBox1.DisplayMember = "PhoneNumber"; 
listBox1.ValueMember = ... ; // value member 
listBox1.DataSource = list; 
+0

嗯,你是對的。我只是將list <>提供給listbox數據源。我以前做過但沒有成功,但當時我沒有與IEnumerable接口的PCPhone類。 – ArtK

+0

這不是必須可枚舉的PCPhone,只是列表。 – 2013-11-21 20:45:40

相關問題