This is a migrated thread and some comments may be shown as answers.

GridViewComboBoxColumn binding to Entity

8 Answers 267 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 17 Oct 2014, 12:27 AM
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!

8 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 17 Oct 2014, 02:50 PM
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.

 
0
Eric
Top achievements
Rank 1
answered on 17 Oct 2014, 06:10 PM
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.
0
Dimitrina
Telerik team
answered on 20 Oct 2014, 11:27 AM
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.

 
0
Eric
Top achievements
Rank 1
answered on 21 Oct 2014, 01:56 AM
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. 
0
Dimitrina
Telerik team
answered on 22 Oct 2014, 07:53 AM
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.

 
0
Eric
Top achievements
Rank 1
answered on 23 Oct 2014, 02:59 AM
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. 



0
Dimitrina
Telerik team
answered on 23 Oct 2014, 10:50 AM
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.

 
0
Eric
Top achievements
Rank 1
answered on 23 Oct 2014, 05:01 PM
This is an extremely common scenario...
Tags
GridView
Asked by
Eric
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Eric
Top achievements
Rank 1
Share this question
or