我有一个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; ¿¿¿ ??? } }
有任何想法吗?