2014-10-29 65 views
0

嘗試綁定列表框c BindingSource。綁定列表框seletedltems

有這樣一個實體

// Entitys 
public class Sp 
{ 
    public int Id { get; set; } 
    public int? IdTemp { get; set; } //Foreing Key 
    public Temp Temp { get; set; } 
    public string Name { get; set; } 
} 
public class Temp 
{ 
    public int Id { get; set; } 
    public string Caption { get; set; } 
    public ICollection<Sp> Sps { get; set; } 
} 

創建的BindingSource

var b = new BindingSource{ DataSource = new BindingList<Temp>().ToList() }; 

創建結合

listBox1.SelectionMode = SelectionMode.MultiSimple; 
//Throws an error: 'SelectedItems' because it is read-only 
listBox1.DataBindings.Add("SelectedItems", B, "Sps.IdTemp", true); 

回答

0

檢查如何使用DataSourceDataBindings正確這樣的回答: How to add new item into listbox that using datasource?

將任何東西綁定到ListBoxSelectedItems屬性是沒有意義的。 相反,您可以將DataSource中的某些內容與Text屬性或類似內容綁定。