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";
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.
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.