I have created a listbox based on a database table where the DisplayMember items of the listbox are created via string concatenation and the ValueMember items represents the bigInt PK from the table. The listbox is bound to a Text/Value object as shown below.
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;
}
}
The listbox is populated and displays correctly but when I attempt to set the SelectedValue property via the code below the listbox's SelectedValue changes to null.
lstCompany.SelectedValue = 16844;