ComboBox DataSource And Entity FrameWork
Solution 1
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.
Solution 2
you need to provide the valuemember property. ValueMember
cmoAuthors.DisplayMember = "FirstName";
cmoAuthors.ValueMember = "yourValueProperty";
Moslem .
Updated on April 10, 2020Comments
-
Moslem . about 4 years
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 , butcmoAuthors.Items.Count
returns 0 butcmoAuthors.DataSource.Count
returns 2 itempublishContext = new publishingCompanyEntities(); cmoAuthors.DataSource = publishContext.Authors; cmoAuthors.DisplayMember = "FirstName";