如何检索绑定匿名列表的comobobox(winform)中显示的文本?
在我的 .net 3.5 win-form 应用程序中,我将一个组合框与这些语句绑定在一起。
using (var db = new NewspaperDataContext())
{
var list = from p in db.Customers
orderby p.Name ascending
select new
{
p.Id,
p.Name
};
cboCustReport.DataSource= list;
cboCustReport.DisplayMember = "Name";
cboCustReport.ValueMember = "Id";
cboCustReport.SelectedIndex = -1;
}
但是,在从组合框中检索选定的文本时,我是“”(空),如果我使用 SelectedItem 属性,那么我有 {Id= 3 , Name = Amit Ranjan }。当我添加一个手表并尝试构建表达式时,它给了我这样的信息:
((<>f__AnonymousType2<int,string>)(cboCustReport.SelectedItem)).Name;
请帮助我,我应该使用什么来获取属性名称的值。
In my .net 3.5 win-form app, i am binding a combo box with these statements.
using (var db = new NewspaperDataContext())
{
var list = from p in db.Customers
orderby p.Name ascending
select new
{
p.Id,
p.Name
};
cboCustReport.DataSource= list;
cboCustReport.DisplayMember = "Name";
cboCustReport.ValueMember = "Id";
cboCustReport.SelectedIndex = -1;
}
But while retrieving the selected text from the combo box, i am ""(empty), If I use SelectedItem property, then I have {Id= 3 , Name = Amit Ranjan }. When I added a watch and tried to build the expression, It gave me something like this:
((<>f__AnonymousType2<int,string>)(cboCustReport.SelectedItem)).Name;
Please help me, what should I use to get the value of property name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试通过在末尾调用
.ToList()
来急切地执行查询:Try to eagerly execute the query by calling
.ToList()
at the end: