ComboBox DataSource And Entity FrameWork

c# entity-framework winforms

Question

I add Data Model Entity to my project named publishingCompanyEntities And added ComboBox in my winform. but when i want to bind my list of authors into my combo box as data source has been filled with data , but cmoAuthors.Items.Count returns 0 but cmoAuthors.DataSource.Count returns 2 item

        publishContext = new publishingCompanyEntities();
        cmoAuthors.DataSource = publishContext.Authors;
        cmoAuthors.DisplayMember = "FirstName";
1
4
4/9/2013 9:58:50 AM

Accepted Answer

You need to add .ToList() to the Authors EntitySet.

publishContext = new publishingCompanyEntities();
        cmoAuthors.DataSource = publishContext.Authors.ToList();
        cmoAuthors.DisplayMember = "FirstName";
        cmoAuthors.Invalidate();

The reason is that an EntitySet is not a actual collection. It's a query (IQueryable), and it seems that the ComboBox is not smart enought to detect that.

Calling the ToList() materialize the publishContext.Authors into objects.

For some reason, the ComboBox does not update it Items Collection, then a new DataSource is detected. Invalidate() forces the Control to redraw iself, and in the process, updating its Items collection.

14
4/9/2013 10:40:32 AM

Popular Answer

you need to provide the valuemember property. ValueMember

cmoAuthors.DisplayMember = "FirstName";
cmoAuthors.ValueMember = "yourValueProperty";


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow