我有一个ComboBox,其中填充了匿名类型的集合:
var results = (from row in data.Tables[0].AsEnumerable()
select new {
Id = row.Field<int>("id"),
Name = row.Field<string>("Name
}).Distinct();
myComboBox.ValueMember = "Id";
myComboBox.DisplayMember = "Name";
foreach (var n in results)
{
myComboBox.Items.Add(n);
}
然后,在comboBox的SelectedIndexChanged方法中,我想检索所选项目的Id,但是我无法访问“Id”属性,在myComboBox.SelectedItem中是所选对象。
private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myComboBox.SelectedItem != null)
{
var x = myComboBox.SelectedItem;
¿¿¿ ???
}
}
有什么想法吗?
Tuple<int, string>
,它比KeyValuePair
更通用。 - neontapir