我有一个名为 listbox1 的列表框将限制为这样的列表:
ValueVM word1 = new ValueVM { Id = 1, Name = "AAA" };
ValueVM word2 = new ValueVM { Id = 2, Name = "XBB" };
ValueVM word3 = new ValueVM { Id = 3, Name = "ACC" };
ValueVM word4 = new ValueVM { Id = 4, Name = "ACB" };
ValueVM word5 = new ValueVM { Id = 5, Name = "OTD" };
ValueVM word6 = new ValueVM { Id = 6, Name = "FDD" };
var li = new List<ValueVM>() { word1, word2, word3, word4, word5, word6 };
listBox1.DataSource = li.OrderBy(l=>l.Name).ToList();
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
我想在此列表框中选择一项,假设我想要 Id=2;
int myID = 2;
//Idont know what the selected index will be but I need the selected value to be set
// I tried to set listBox1.SelectedValue=myId.ToString();
//but still returning null
listBox1.SelectedValue = myID;
如 MSDN 中所述:
获取或设置由 ValueMember 属性指定的成员属性的值。(继承自 ListControl。)
我的问题是选择值从值成员获取值但没有设置值?有任何想法吗?