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:
- Will populate its list from DataTable dtList, and display in the dropdown the strings Country (not its Primary Key L_ID)
- 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
- FTELOCComboBox is bound in code behind to: FTELOCComboBox.ItemsSource = ViewModel.FTE_LocationDataTable;
- And ViewModel.FTE_LocationDataTable is public DataTable FTE_LocationDataTable {get { return _FTE_Location; }} and _FTE_Location is a DataTable
- 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