0
我已經創建了一個基於數據庫表的列表框,其中列表框的DisplayMember項是通過字符串連接創建的,而ValueMember項表示表中的bigInt PK。列表框被綁定到文本/值對象,如下所示。設置SelectedValue後,C#Listbox值爲空
List<ComboSearchItems> csi = new List<ComboSearchItems>();
foreach(var i in q)
{
ComboSearchItems ci = new ComboSearchItems(String.Concat(i.Id, " - ", i.Name, " - ", i.CompanyName), i.Id);
csi.Add(ci);
}
lstCompany.DataSource = csi;
lstCompany.DisplayMember = "Text";
lstCompany.ValueMember = "Value";
lstCompany.SelectedIndex = 0;
lstCompany.Refresh();
public class ComboSearchItems
{
public string Text { get; set; }
public Int64 Value { get; set; }
//Constructor
public ComboSearchItems(string t, Int64 v)
{
Text = t;
Value = v;
}
}
列表框填充和顯示正確,但是當我試圖通過下面的列表框中的的SelectedValue變化的代碼來設置SelectedValue屬性爲null。
lstCompany.SelectedValue = 16844;