How to: Bind Controls to Derived Types
Entity Framework enables you to bind Windows Forms controls such as a ComboBox or DataGridView to an EntityCollection. However, when you execute the OfType method on an EntityCollection to return a collection of objects of a derived type, you cannot bind the returned IEnumerable directly to a control. The example in this topic shows how to use the CreateSourceQuery method to create an ObjectQuery that defines the EntityCollection. This query is executed with the OfType method to perform the binding to a specific subtype. For more information, see Binding Objects to Controls.
The example in this topic is based on a modified version of the School model. This version supports table-per-type inheritance with Course as an abstract type. Complete the Mapping Inheritance - Table-per-Type walkthrough to modify the School model to support the table-per-type inheritance example used in this topic.
The following example is from a Windows Form. When the form is loaded, an ObjectResult of Department objects is returned by calling the Execute method of the ObjectQuery. This result is bound to a combo box. When an order is selected, the CreateSourceQuery method is called on the related EntityCollection of Course objects. The returned ObjectQuery executed using the OfType method and the result is bound to a DataGridView control. The type that is used with the OfType method is set based on a radio-box setting in the form.