ComboBox DataSource And Entity FrameWork

21,304

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";
Share:
21,304
Moslem .
Author by

Moslem .

Updated on April 10, 2020

Comments

  • Moslem .
    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 , but cmoAuthors.Items.Count returns 0 but cmoAuthors.DataSource.Count returns 2 item

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