开发者

How to retrieve the text displayed in the comobobox (winform) binded with anonymous list?

开发者 https://www.devze.com 2023-04-11 12:11 出处:网络
In my .net 3.5 win-form app, i am binding a combo box with these statements. using (var db = new NewspaperDataContext())

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,s开发者_开发技巧tring>)(cboCustReport.SelectedItem)).Name;

Please help me, what should I use to get the value of property name.


Try to eagerly execute the query by calling .ToList() at the end:

var list = (from p in db.Customers
            orderby p.Name ascending
            select new
            {
                p.Id,
                p.Name
            }).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号