GridViewComboBoxColumn binding to Entity

9 posts, 0 answers
  1. Eric
    Eric avatar
    36 posts
    Member since:
    Apr 2013

    Posted 16 Oct 2014 Link to this post

    I have a datagrid filled from an entity framework data model. 

    One of the columns is a combobox and I need to bind the SelectedValue of the combobox to the navigation property of the entity and I need to bind the SelectedIndex to the ID. If I can't bind to both of those properties and keep them consistent (ID matches the value in the database) then Entity Framework is a pain to work with.

    It's not hard to do with a standard WPF combobox as explained here:

    http://www.codeproject.com/Articles/301678/Step-by-Step-WPF-Data-Binding-with-Comboboxes 

    Here is the closes I've gotten: 

    <telerik:GridViewComboBoxColumn Header="Units"                                                                                                                   ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.UnitsOfMeasure}"                                                                                                                           DisplayMemberPath="Name"                                                                                                         DataMemberBinding="{Binding UnitOfMeasureID}"                                                                                     SelectedValueMemberPath="Id"/>

    It Binds to the UnitsOfMeasure collection as the items source. It displays the name of the units as it should, and the UnitOfMeasureID in the datagrid row member is properly bound to the Id of the selected item from the combobox. The thing I can't seem to figure out is how to get the UnitOfMeasure object bound to the SelectedValue object of the combo box.

    Thanks!

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 Oct 2014 Link to this post

    Hi,

    You can check the article on ComboBox Column to better understand how you should configure the GridViewComboBoxColumn. You can also check some demos on using GridViewComboBoxColumn in our SDK Samples Browser.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Eric
    Eric avatar
    36 posts
    Member since:
    Apr 2013

    Posted 17 Oct 2014 in reply to Dimitrina Link to this post

    I did the binding explained in the documentation and it did not fit my scenario. I'll take a look through the SDK Samples Browser.
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 20 Oct 2014 Link to this post

    Hello,

    I hope the examples will help you achieve your goal. Let me know if any additional questions arise.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Eric
    Eric avatar
    36 posts
    Member since:
    Apr 2013

    Posted 20 Oct 2014 in reply to Dimitrina Link to this post

    I have gone through all of your gridview combo box samples and none of them address the concept that with entity framework I need to bind to both the ID value of the class as well as the navigation property of the class. 
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Oct 2014 Link to this post

    Hello,

    I am not sure I understand your requirement right. Basically, the way you can configure our column is as explained in ComboBox Column article.

    As another approach, I can suggest you to define your own CellTemplate for the column and configure a ComboBox control as you wish. 
    You can also check the following article in our online documentation suggesting how to do so: Setting CellTemplate and CellEditTemplate.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Eric
    Eric avatar
    36 posts
    Member since:
    Apr 2013

    Posted 22 Oct 2014 in reply to Dimitrina Link to this post

    Here is an example of a bound DataGridComboBoxColumn:

    <DataGridComboBoxColumn Header="Units" 
                                                        ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.UnitsOfMeasure}"
                                                        DisplayMemberPath="Name"
                                                        SelectedItemBinding="{Binding Units}"
                                                        SelectedValueBinding="{Binding UnitsID}"
                                                        SelectedValuePath="Id"/>

    Note that there are both a selectedItemBinding and a SelectedValueBinding. So, if I have a class made from entity framework and it is based off of a database table with a foreign key constraint to a "units" table, that class will have a "UnitsID" property and a "Units" property. The "UnitsID" property is just an integer while the "Units" Property is the "Units" instance where its "ID" matches "UnitsID" in the first class. 

    Those two properteis, "SelectedItemBinding" and "SelectedValueBinding" let me bind to both the ID and the Units item itself simultaneously which is very important when working with entity framework because if I change one but not the other then EntityFramework does not know which one I want and throws an exception when I try to save any changes to the class. 

    Here is the corresponding GridViewTemplateColumn:                           

    <telerik:GridViewComboBoxColumn Header="Units" 
                                                                ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.UnitsOfMeasure}"
                                                                DisplayMemberPath="Name"
                                                                DataMemberBinding="{Binding UnitOfMeasureID}"                                          
                                                                SelectedValueMemberPath="Id"/>

    Note that it is one property short and so I cannot databind it to a class from entity framework properly. 



  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 23 Oct 2014 Link to this post

    Hello,

    I am afraid our GridViewComboBoxColumn cannot be configured that way. As it turns out internal implementation relies on just one DataMemberBinding.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Eric
    Eric avatar
    36 posts
    Member since:
    Apr 2013

    Posted 23 Oct 2014 in reply to Dimitrina Link to this post

    This is an extremely common scenario...
Back to Top