[英]C# winForm Basics Combobox
hey all, i've built a ComboBox that gets manually items like this: 嘿,我建立了一个ComboBox,它可以手动获取以下内容:
var newitem = new { fullname =c.Company+" "+ c.FirstName + " " + c.LastName,
custId = c.CustomerID };
c_dropCustomers.Items.Add(newitem);
later on on combo Selection event, i would want to get out the custId (The Value) only but i dont know how to reach it. 稍后在组合选择事件上,我只想获取custId(值),但我不知道如何实现。
SOS :) SOS :)
asuming c# 4.0: 假设C#4.0:
dynamic item = c_dropCustomers.SelectedItem;
dynamic customerID = item.custId;
You need to actually define a class, so that you can cast to it later. 您实际上需要定义一个类,以便以后可以强制转换为它。 You can't cast to an anonymous class (AFAIK). 您不能转换为匿名类(AFAIK)。
If I understand your question correctly: 如果我正确理解您的问题:
var item = c_dropCustomers.SelectedItem;
var custId = item.custId;
EDIT: (C# 3.5) 编辑: (C#3.5)
If it really is an anonymous type you'll need to do something like this: 如果确实是匿名类型,则需要执行以下操作:
ComboBox cb = new ComboBox();
cb.Items.Add(new { fullname = "Company" + " " + "First Name" + " " + "Last Name", custId = 44 });
cb.SelectedIndex = 0;
var item = cb.SelectedItem;
var custId = item.GetType().GetProperty("custId").GetValue(item, System.Reflection.BindingFlags.GetProperty, null, null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.