Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox DataSource And Entity FrameWork

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";
like image 217
Moslem . Avatar asked Dec 02 '22 19:12

Moslem .


1 Answers

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.

like image 160
Jens Kloster Avatar answered Dec 24 '22 14:12

Jens Kloster