Failure to bind to SfDataGrid using CollectionViewSource (VS 2013)
I have been leveraging Entity Framework (EF) to build Entities from the world famous Northwind Product and Categories Table.
Steps
1. I create the Product and Category EF objects from the Northwind database.
2. I use drag and drop from the Data Source Window to create a WPF DataGrid for the Products Table. This creates CollectionViewSource for the Products Table and puts a reference in the MainWindow.xaml Resources.
3. I do the same (step 2) for the Category Table. But in the case of the Category Table I delete the WPF DataGrid created for the Category Table. The purpose of this step is to crated the CollectionViewSource for the Category Table.
4. I update the code behind to fully populate the CollectionViewSource for both the Product and Categories Table.
5. I modify the columns created for the Products DataGrid that was created in step 2. This DataGrid is reduced to 2 columns: a DataGridTextColumn bound to ProductName and a DataGridComboBox used to select Category. The DataGridComboBox is written from scratch. Compile and Run - success. I can change the Category.
6. I comment out the WPF DataGrid
7. I try to create a functionally equivalent Syncfusion SfDataGrid, but I cannot get the GridComboBoxColumn to work. See Below. Nothing shows in the combo box. If I change the DisplayMemberPath to "CategoryID", it show that value.
<!--<DataGrid x:Name="productDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
Margin="43,32,36,163" ItemsSource="{Binding}"
EnableRowVirtualization="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="productNameColumn"
Header="Product" Binding="{Binding ProductName}"/>
<DataGridComboBoxColumn x:Name="categoryColumn"
Header="Category" ItemsSource="{Binding Source={StaticResource
categoryViewSource}}"
SelectedValuePath="CategoryID" SelectedValueBinding="{Binding
CategoryID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="CategoryName" />
</DataGrid.Columns>
</DataGrid>-->
<Syncfusion:SfDataGrid x:Name="productDataGrid"
Margin="43,32,36,163" ItemsSource="{Binding}"
AutoGenerateColumns="False" AllowEditing="True">
<Syncfusion:SfDataGrid.Columns>
<Syncfusion:GridTextColumn x:Name="productNameColumn" HeaderText="Product"
MappingName="ProductName"/>
<Syncfusion:GridComboBoxColumn x:Name="categoryColumn"
HeaderText="Category"
ItemsSource="{Binding Path=Category, Source={StaticResource categoryViewSource},
Mode=TwoWay}"
MappingName="CategoryName" AllowEditing="True" />
</Syncfusion:SfDataGrid.Columns>
</Syncfusion:SfDataGrid>
Thank You for the help
Attachment: Wpf_NWND_58823852.zip
Thanks for contacting Syncfusion support.
We have analyzed your query and your sample is not compiled in our machine. However we have prepared sample as per your XAML code, but GridComboBoxColumn works fine in our side. Which loads the data as expected. And if you want to load the selected item of the combobox in edit mode, you have to set DisplayMemberPath or SelectedValuePath of GridComboboxColumn.
Please refer the attached sample that we have tested as per your scenario.
Sample Link: http://www.syncfusion.com/downloads/support/forum/123381/ze/ComboBoxColumns_-_WPF-1652008946
Please refer the below UG Link to know more about GridComboBoxColumn,
Link: http://help.syncfusion.com/wpf/sfdatagrid/column-types?cs-save-lang=1&cs-lang=xaml#gridcomboboxcolumn
Regards,
Srinivasan
<StackPanel>
<DataGrid x:Name="productDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10"
ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="productNameColumn" Header="Product"
Binding="{Binding ProductName}" Width="Auto"/>
<DataGridComboBoxColumn x:Name="categoryColumn" Header="Category"
ItemsSource="{Binding Source={StaticResource categoryViewSource}}"
SelectedValuePath="CategoryID" SelectedValueBinding="{Binding CategoryID,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="CategoryName" />
</DataGrid.Columns>
</DataGrid>
AutoGenerateColumns="False" AllowEditing="True" AllowResizingColumns="True" ColumnSizer="Auto">
<Syncfusion:SfDataGrid.Columns>
<Syncfusion:GridTextColumn x:Name="productNameColumn1" HeaderText="Product"
DisplayBinding="{Binding Path=ProductName}" AllowResizing="True" />
<Syncfusion:GridComboBoxColumn x:Name="categoryColumn1" HeaderText="Category"
ItemsSource="{Binding Source={StaticResource productViewSource}}"
MappingName="CategoryID" AllowEditing="True"
DisplayMemberPath="CategoryName"
/>
</Syncfusion:SfDataGrid.Columns>
</Syncfusion:SfDataGrid>
</StackPanel>
</Grid>
Hi Srinivasan,I tried both of your suggestions to no avail. I changed the XAML in the SfDataGrid that I sent in opening post. The DataGrid works - but cannot figure out how to get SfDataGrid to work.<Grid DataContext="{StaticResource productViewSource}">
<StackPanel>
<DataGrid x:Name="productDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10"
ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="productNameColumn" Header="Product"
Binding="{Binding ProductName}" Width="Auto"/>
<DataGridComboBoxColumn x:Name="categoryColumn" Header="Category"
ItemsSource="{Binding Source={StaticResource categoryViewSource}}"
SelectedValuePath="CategoryID" SelectedValueBinding="{Binding CategoryID,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="CategoryName" />
</DataGrid.Columns>
</DataGrid><Syncfusion:SfDataGrid x:Name="productDataGrid1" Margin="10" ItemsSource="{Binding}"
AutoGenerateColumns="False" AllowEditing="True" AllowResizingColumns="True" ColumnSizer="Auto">
<Syncfusion:SfDataGrid.Columns>
<Syncfusion:GridTextColumn x:Name="productNameColumn1" HeaderText="Product"
DisplayBinding="{Binding Path=ProductName}" AllowResizing="True" />
<Syncfusion:GridComboBoxColumn x:Name="categoryColumn1" HeaderText="Category"
ItemsSource="{Binding Source={StaticResource productViewSource}}"
MappingName="CategoryID" AllowEditing="True"
DisplayMemberPath="CategoryName"
/>
</Syncfusion:SfDataGrid.Columns>
</Syncfusion:SfDataGrid>
</StackPanel>
</Grid>I think the main SfDataGrid problem I am grappling with is the ItemSource Binding Path required by Syncfusion. I cannot figure out what to use as a Path for the CollectionViewSource. The example you sent was for an Observable Collection. By trial and error, I cannot figure out what to do.In my XAML above for the SfDataGrid, where I do not use any Item Source Binding Path, the combobox columns comes up blank yet I can select from the pull down Category Names in the combo box. The combo box selection does not stick and reverts to blank when I exit. If I use any value (and I have tried several) as the Binding Path I regress & cannot even see Category Names in the pull down.As I stated earlier I use EF and Drag & Drop from DataSource window to build my CollectionViewSource objects & references to it.
<StackPanel>
<DataGrid x:Name="productDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10"
ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="productNameColumn" Header="Product"
Binding="{Binding ProductName}" Width="Auto"/>
<DataGridComboBoxColumn x:Name="categoryColumn" Header="Category"
ItemsSource="{Binding Source={StaticResource categoryViewSource}}"
SelectedValuePath="CategoryID" SelectedValueBinding="{Binding CategoryID,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="CategoryName" />
</DataGrid.Columns>
</DataGrid>
AutoGenerateColumns="False" AllowEditing="True" AllowResizingColumns="True" ColumnSizer="Auto">
<Syncfusion:SfDataGrid.Columns>
<Syncfusion:GridTextColumn x:Name="productNameColumn1" HeaderText="Product"
DisplayBinding="{Binding Path=ProductName}" AllowResizing="True" />
<Syncfusion:GridComboBoxColumn x:Name="categoryColumn1" HeaderText="Category"
ItemsSource="{Binding Source={StaticResource categoryViewSource}}"
MappingName="CategoryID" AllowEditing="True"
DisplayMemberPath="CategoryName"
/>
</Syncfusion:SfDataGrid.Columns>
</Syncfusion:SfDataGrid>
</StackPanel>
</Grid>
Thanks for contacting Syncfusion support.
We have analyzed your query and in EditMode we have load the Microsoft ComboBox control in our SfDataGrid.GridComboboxColumn. Hence, all the default Microsoft ComboBox behaviors will applicable for GridComboBoxColumn.
As per your code, the same issue is replicated while using ComboBox seperately. So, we have used alternate way to bind CollectionViewSource with SfDataGrid.GridComboboxColumn and prepared a sample accordingly.
We have used ICollectionViewSource instead of CollectionViewSource. In ViewModel we have defined two properties called “Employee” and “ProductsView” to load items in SfDataGrid as well as in GridComboBoxColumn. And you can download the same from the following location.
Sample: http://www.syncfusion.com/downloads/support/forum/123381/ze/CollectionViewSource2113950577
Regards,
Srinivasan
- L_ID as Primary Key
- Country as string
- ID as primary key
- L_ID as foreign key of datatable dtList.
- populate its list from DataTable dtLIST, and display in the dropdown the strings Country
- 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
Ok, I have this working using the below However the column MyHeaderText, always shows blank (nothing, empty), until I click on the cell into edit mode. When it goes to edit mode it brings up the ComboBox and the combobox works. However, when i then move off the cell, the cell becomes blank again.
Why does this happen? I downloaded the example attached to the this same thread earlier, and it doesn't happen in that example. There are a few other people who have mentioned this in other posts. But I have never found a resolution
WORKING XMAL
<syncfusion:GridComboBoxColumn
x:Name="FTELOCComboBox" //binding is set in the code behind to this object as --> FTELOCComboBox.ItemsSource = FTE_LocationDataTable.DefaultView; (and FTE_LocationDataTable is a DataTable)
DisplayMemberPath="Country"
HeaderText="MyHeaderText"
MappingName="L_ID"
SelectedValuePath="ID
- L_ID as Primary Key
- Country as string
- ID as primary key
- L_ID as foreign key of datatable dtList.
- 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
- FTELOCComboBox is bound in code behind to: FTELOCComboBox.ItemsSource = ViewModel.FTE_LocationDataTable;
- And ViewModel.FTE_LocationDataTable is as below
- public DataTable FTE_LocationDataTable { get { return _FTE_Location; } }
- The binding ItemsSource="{Binding dtMaster}" is the property public DataTable DTMaster { get { return dtMaster; }} where dtMaster is a DataTable
- 10 Replies
- 4 Participants
-
JM john mullally
- Mar 12, 2016 07:48 AM UTC
- Jun 9, 2016 09:24 AM UTC