Editable Master/Detail SfDataGrid with GridComboBoxColumn

Hello,

I am trying to create an editable SfDataGrid with a Master/Details layout with custom columns. Both on the master and the details layouts I use a GridComboBoxColumn that is connected to a SmartEnum property type. I can populate the items for the GridComboBoxColumn ItemsSource, but I am unable to set the property when selecting from the combobox. The sample includes a screenshot.

I have attached my xaml code for the datagrid, the data classes, the itemssource property from the viewmodel and the IValueConverter.

- The Master datagrid has an itemssource which is an ObservableCollection<PatientPregnancyDisplayModel>.

- Each  PatientPregnancyDisplayModel has a List<ATermeDateModel>, which is the itemssource for the Details datagrid.  I wonder if this should also be an ObservableCollection  rather than a list?

- The property 'DeterminationMethod' is a of the 'ATermeDateModel' class. It is of type SmartEnum (https://github.com/ardalis/SmartEnum) and I would like to display it as a combobox selection on the datagrid.

 I am trying to follow the explanation on this page: https://www.syncfusion.com/kb/11999/how-to-set-the-default-display-text-for-gridcombobox-column-in-wpf-datagrid-sfdatagrid but I am unable to set the property and am unsure what I am doing wrong.

Any help is appreciated.

Kind regards,

Niels van Strien



Attachment: demo_51c274df.zip

4 Replies

VS Vijayarasan Sivanandham Syncfusion Team February 10, 2022 03:30 PM UTC

Hi NM van Strien,

We have checked the provided code snippet from our end. We suspect that your requirement to bind enum as ItemSource to GridComboBoxColumn in DetailsViewDataGrid. Selected value should be updated properly in GridComboBoxColumn of SfDataGrid. 

Your requirement to bind an Enum to the GridComboBoxColumn as an Itemsource in SfDataGrid can be achieved by using ObjectDataProvider like below mentioned code snippet, 

XAML Code Snippet: 

<Window.Resources> 
        <ObjectDataProvider x:Key="EnumList" 
                            MethodName="GetValues" 
                            ObjectType="{x:Type sys:Enum}"> 
            <ObjectDataProvider.MethodParameters> 
                <x:Type TypeName="local:SmartEnum" /> 
            </ObjectDataProvider.MethodParameters> 
        </ObjectDataProvider> 
</Window.Resources> 
 
<syncfusion:SfDataGrid x:Name="dataGrid" 
                                       AutoGenerateColumns="False" 
                                       AutoGenerateRelations="False" 
                                       AllowEditing="True" 
                                       LiveDataUpdateMode="AllowDataShaping"                                          
                                       ItemsSource="{Binding Path=OrdersDetails}" 
                                       NavigationMode="Cell" 
                                       ShowGroupDropArea="True"> 
            <syncfusion:SfDataGrid.DetailsViewDefinition> 
                <syncfusion:GridViewDefinition RelationalColumn="OrderDetails"> 
                    <syncfusion:GridViewDefinition.DataGrid> 
                        <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid"  AllowEditing="True" 
                                        AutoGenerateColumns="True"> 
                            <syncfusion:SfDataGrid.Columns> 
                                <syncfusion:GridTextColumn MappingName="OrderID" /> 
                                <syncfusion:GridComboBoxColumn HeaderText="Methode" 
                                          ItemsSource="{Binding Source={StaticResource  EnumList}}" 
                                          MappingName="DeterminationMethod" /> 
                                <syncfusion:GridTextColumn MappingName="ProductID" TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn HeaderText="Unit Price" 
                                                                   MappingName="UnitPrice" 
                                                                   TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn MappingName="Quantity" TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn MappingName="Discount" TextAlignment="Right" /> 
                                <syncfusion:GridTextColumn MappingName="CustomerID" /> 
                                <syncfusion:GridDateTimeColumn HeaderText="Order Date" 
                                                                       MappingName="OrderDate" 
                                                                       TextAlignment="Right" /> 
                            </syncfusion:SfDataGrid.Columns> 
                        </syncfusion:SfDataGrid> 
                    </syncfusion:GridViewDefinition.DataGrid> 
                </syncfusion:GridViewDefinition> 
            </syncfusion:SfDataGrid.DetailsViewDefinition> 
</syncfusion:SfDataGrid> 

C# Code Snippet: 
/// <summary> 
 /// Gets or sets the Determination Method. 
/// </summary> 
 /// <value>The Determination Method.</value> 
public SmartEnum DeterminationMethod 
{ 
            get 
            { 
                return this.determinationMethod; 
            } 
            set 
            { 
                this.determinationMethod = value; 
                RaisePropertyChanged("DeterminationMethod"); 
            } 
} 

Regards, 
Vijayarasan S 



NV NM van Strien February 10, 2022 06:20 PM UTC

Hi,

Thank you for your help. I have gotten it to work.


If I may ask another question -  I have trouble finding the SelectedItem on the DetailsDataGrid.

I have created a SelectedItem on the Master datagrid in xaml and bound it to the ViewModel. Every time a selection changes, the correct SelectedItem on the Master Datagrid is updated.


However, when I select an item on the DetailsDataGrid , the same SelectedItem gets triggered as if I were selecting an item in the Master datagrid.


When adding an eventhandler to the cs file behind the xaml for the  DetailsDataGrid SelectionChanged, it is consistently triggered - however I am not sure how to access this from the ViewModel in an MVVM approach.


Can you point me to an example where the SelectedItem of the DetailsDataGrid is accessed from the ViewModel in an MVVM approach?


Kin




NV NM van Strien February 10, 2022 07:56 PM UTC

I think I found it - I added a trigger in the xaml view through which I could programatically access the DetailsGrid from the ViewModel. Then I added an eventhandler for the SelectionChanged event on the ViewModel.


Thanks againfor all your help



VS Vijayarasan Sivanandham Syncfusion Team February 11, 2022 05:30 AM UTC

Hi NM van Strien,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon