We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GridComboBoxColumn bound to DataTable, Primary Key inserted as Foreign Key in Parent DataTable

GridComboBoxColumn bound to DataTable, Primary Key inserted as Foreign Key in Parent DataTable

This took me over 10 hours to get working. This post is not a request for help, it is to document what I did should any other travelers come looking for the same soluiton


Requirement
If I have a DataTable dtList that has two columns
  • L_ID as Primary Key
  • Country as string

And if I have a datatable dtMaster that has a foreign key relationship to dtList with columns as below:
  • ID as primary key
  • L_ID as foreign key of DataTable dtList, (defined above)

I wish to create a  GridComboBoxColumn that:
  1. Will populate its list from  DataTable dtList, and display in the dropdown the strings Country (not its Primary Key L_ID)
  2. When the user selects a country, the combobox then takes the  L_ID as selected value and inserts that foreign key L_ID into DataTable dtMaster

XAML  
 <syncfusion:SfDataGrid x:Name="productDataGrid"
                               AllowEditing="True"
                               AutoGenerateColumns="False"
                               ColumnSizer="Auto"                            
                               ItemsSource="{Binding DTMaster}"                               
                               ShowGroupDropArea="True">
                        <syncfusion:GridComboBoxColumn 
                                                x:Name="FTELOCComboBox"  //See note below
                                                DisplayMemberPath="Country"
                                               HeaderText="MyHeaderText"
                                               MappingName="L_ID"
                                               SelectedValuePath="L_ID" />

Where 
  1. FTELOCComboBox is bound in code behind to: FTELOCComboBox.ItemsSource = ViewModel.FTE_LocationDataTable;
  2. And ViewModel.FTE_LocationDataTable is  public DataTable FTE_LocationDataTable {get { return _FTE_Location; }} and _FTE_Location is a DataTable
  3. The binding   ItemsSource="{Binding dtMaster}" is the property public  DataTable DTMaster  { get { return dtMaster; }} where dtMaster is a DataTable

Trap
The trap that cost me hours is that I bound to the DefaultView of the datatable FTELOCComboBox.ItemsSource=ViewModel.FTE_LocatationDataTable.DefaultView

It seemed natural to bind to DefaultView, for displaying a DataTable in a grid, as this is what is mostly done. I didn't suspect it as the cause of this whole thing not working causing well over 10 hours of mucking around. 

James


2 Replies

JA James June 8, 2016 03:03 PM UTC

James here again, my apologies in the above post I accidentally changed the name of the DataTable dtList in the 'Requirements' to _FTE_Locatation in the code. These are the same. I wish I could edit my original post. 



JG Jai Ganesh S Syncfusion Team June 9, 2016 09:25 AM UTC

Hi James, 
 
Thank you for the update. Please let us know if you need any other assistance 
 
Regards, 
Jai Ganesh S 


Loader.
Live Chat Icon For mobile
Up arrow icon