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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind different ItemsSources to each row of the GridComboBoxColumn?

Platform: UWP |
Control: SfDataGrid |
Published Date: March 27, 2018 |
Last Revised Date: March 27, 2018 |
.NET Framework: 4.5

You can bind the different ItemsSource to each row of GridComboBoxColumn by setting ItemsSourceSelector property.

XAML

 

Implementing IItemsSourceSelector

ItemsSourceSelector needs to implement IItemsSourceSelector interface which requires you to implement GetItemsSource method which receives the below parameters,

  1. Record – data object associated with row.
  2. Data Context – data context of data grid.

In the below code, ItemsSource for ShipCity column returned based on ShipCountry column value using the record and data context of data grid passed to GetItemsSource method.

C#

 

The following screenshot illustrates the different ShipCity ItemsSource bound to each row of the ComboBox based on the Country Name.

Figure 1: ShipCity list of Argentina

 

Figure 2: ShipCity list of Belgium

 

Sample

WPF

UWP

2X faster development

The ultimate UWP UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Szántai László
Aug 05, 2020

Xamarin example?

Reply
Adriano Arroyo
Sep 22, 2021

Hi there. This example can be applied in a grid with GridViewDefinition from c # code? I have tried to implement ItemsSourceSelector in code but failed.

this is my xaml

<syncfusion:SfDataGrid x:Name="grd" ScrollMode="Async" AddNewRowPosition="Bottom" syncfusionskin:SfSkinManager.Theme="{syncfusionskin:SkinManagerExtension ThemeName=MaterialLight}" LostFocusBehavior="Default"

                   AddNewRowText="Crear nuevo"
                               ShowRowHeader="True" 
                               AutoGenerateColumns="False" RowHeight="30"
                               ColumnSizer="Star"
                               NavigationMode="Cell"
                               ShowGroupDropArea="True" 
                               AllowDeleting="True"
                               SelectionUnit="Row"
                               ScrollViewer.VerticalScrollBarVisibility="Visible" 
                               ScrollViewer.HorizontalScrollBarVisibility="Visible" 
                               AddNewRowInitiating="grd_AddNewRowInitiating" 
                               CurrentCellBeginEdit="grd_CurrentCellBeginEdit" 
                               CurrentCellDropDownSelectionChanged="grd_CurrentCellDropDownSelectionChanged" 
                               RowValidating="grd_RowValidating" DetailsViewExpanded="grd_DetailsViewExpanded">



            <syncfusion:SfDataGrid.Columns>

                <syncfusion:GridTextColumn HeaderText="Codigo"  MappingName="_id"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                <!--<syncfusion:GridTextColumn HeaderText="Usuario"  MappingName="_idUsuario"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>-->

                <syncfusion:GridComboBoxColumn x:Name="cmbVehiculo" HeaderText="Vehiculo"  MappingName="_vehiculo._id" SelectedValuePath="_id" DisplayMemberPath="_placa"
                                           AllowEditing="True"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                <syncfusion:GridTextColumn HeaderText="Fecha Ingreso"  MappingName="_fechaIngreso"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                <syncfusion:GridTextColumn HeaderText="Fecha Salida"  MappingName="_fechaSalida"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                <syncfusion:GridTemplateColumn MappingName="Finalizar"
                                            syncfusion:FocusManagerHelper.WantsKeyInput="True" 
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False">

                    <syncfusion:GridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="btnEliminarIngreso" Margin="4,0,4,0" syncfusion:FocusManagerHelper.FocusedElement="True"  
                                                        CommandParameter="{Binding .}" Style="{StaticResource MaterialDesignFlatButton}">
                                <Button.Content>
                                    <StackPanel Orientation="Horizontal">
                                        <materialDesign:PackIcon Grid.Column="0" Grid.Row="0" Height="20" Width="auto" VerticalAlignment="Center" HorizontalAlignment="Center" Kind="CheckAll" Foreground="Black"/>
                                    </StackPanel>
                                </Button.Content>
                            </Button>
                        </DataTemplate>
                    </syncfusion:GridTemplateColumn.CellTemplate>
                </syncfusion:GridTemplateColumn>




            </syncfusion:SfDataGrid.Columns>

            <syncfusion:SfDataGrid.DetailsViewDefinition>
                <syncfusion:GridViewDefinition RelationalColumn="_detalleIngresoTallerr">
                    <syncfusion:GridViewDefinition.DataGrid>
                        <syncfusion:SfDataGrid x:Name="grdDetalle" AutoGenerateColumns="False" AddNewRowPosition="Bottom" AddNewRowText="Crear nuevo" 
                                               CurrentCellBeginEdit="grdDetalle_CurrentCellBeginEdit"
                                               RowValidating="grdDetalle_RowValidating"
                                               CurrentCellDropDownSelectionChanged="grdDetalle_CurrentCellDropDownSelectionChanged" CurrentCellValidated="grdDetalle_CurrentCellValidated" CurrentCellValidating="grdDetalle_CurrentCellValidating" DetailsViewUnloading="grdDetalle_DetailsViewUnloading" DetailsViewLoading="grdDetalle_DetailsViewLoading" CurrentCellActivated="grdDetalle_CurrentCellActivated">
                            <!--<syncfusion:SfDataGrid.GroupColumnDescriptions>
                                <syncfusion:GroupColumnDescription ColumnName="_intervenciones._id" />
                            </syncfusion:SfDataGrid.GroupColumnDescriptions>-->
                            <syncfusion:SfDataGrid.Columns>
                                <syncfusion:GridTextColumn HeaderText="Codigo"  MappingName="_id"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                <syncfusion:GridTextColumn HeaderText="Codigo Ingreso"  MappingName="_idIngresoTaller"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                <syncfusion:GridTextColumn HeaderText="id cat"  MappingName="_intervenciones._idCatRepuesto" IsHidden="True"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                <syncfusion:GridComboBoxColumn x:Name="cmbTipoIntervencion" HeaderText="Tipo Intervencion"  MappingName="_intervenciones._id" SelectedValuePath="_id" DisplayMemberPath="_nombre"  
                                           AllowEditing="True"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>


                                <syncfusion:GridTextColumn HeaderText="Fecha Inicio"  MappingName="_fechaInicio"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                <syncfusion:GridTextColumn HeaderText="Fecha Fin"  MappingName="_fechaFinal"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                <syncfusion:GridTemplateColumn x:Name="colEstado" HeaderText="Requerido"  MappingName="_intervenciones._requerido"
                                            syncfusion:FocusManagerHelper.WantsKeyInput="True" 
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False">

                                    <syncfusion:GridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ToggleButton x:Name="tgbRequerido" IsChecked="{Binding _intervenciones._requerido}" Style="{StaticResource MaterialDesignSwitchToggleButton}"></ToggleButton>
                                        </DataTemplate>
                                    </syncfusion:GridTemplateColumn.CellTemplate>
                                </syncfusion:GridTemplateColumn>

                                <syncfusion:GridTemplateColumn MappingName="Eliminar"
                                            syncfusion:FocusManagerHelper.WantsKeyInput="True" 
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False">

                                    <syncfusion:GridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button x:Name="btnEliminarIntervencion" Margin="4,0,4,0" syncfusion:FocusManagerHelper.FocusedElement="True"  
                                                        CommandParameter="{Binding .}" Style="{StaticResource MaterialDesignFlatButton}">
                                                <Button.Content>
                                                    <StackPanel Orientation="Horizontal">
                                                        <materialDesign:PackIcon Margin="0,0,0,6" Height="20" Width="auto" VerticalAlignment="Center" HorizontalAlignment="Center" Kind="CloseBox" Foreground="Black"/>
                                                    </StackPanel>
                                                </Button.Content>
                                            </Button>
                                        </DataTemplate>
                                    </syncfusion:GridTemplateColumn.CellTemplate>
                                </syncfusion:GridTemplateColumn>
                            </syncfusion:SfDataGrid.Columns>

                            <syncfusion:SfDataGrid.DetailsViewDefinition>
                                <syncfusion:GridViewDefinition x:Name="ttes" RelationalColumn="_intervenciones._categoriass._repuestos" >
                                    <syncfusion:GridViewDefinition.DataGrid>
                                        <syncfusion:SfDataGrid x:Name="grdRepuesto" AutoGenerateColumns="False" AddNewRowPosition="Bottom" AddNewRowText="Crear nuevo" ItemsSource="{Binding Path=DataContext, ElementName=grdRepuesto}"
                                                               CurrentCellBeginEdit="grdRepuesto_CurrentCellBeginEdit"
                                                               RowValidating="grdRepuesto_RowValidating"
                                                               CurrentCellDropDownSelectionChanged="grdDetalle_CurrentCellDropDownSelectionChanged" CurrentCellActivated="grdRepuesto_CurrentCellActivated" DetailsViewUnloading="grdRepuesto_DetailsViewUnloading" DetailsViewLoading="grdRepuesto_DetailsViewLoading"
                                                               >
                                            <syncfusion:SfDataGrid.Columns>
                                                <syncfusion:GridTextColumn HeaderText="Codigo"  MappingName="_idDetalle"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                                <syncfusion:GridComboBoxColumn x:Name="cmbRepuesto" HeaderText="Repuesto"  MappingName="_idRepuesto" SelectedValuePath="_idRepuesto" DisplayMemberPath="_nombre" ItemsSourceSelector="{StaticResource itemSourceSelector}"
                                           AllowEditing="True"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                                <syncfusion:GridTextColumn HeaderText="Cantidad"  MappingName="_cantidad"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                                <syncfusion:GridTextColumn HeaderText="Fecha Registro"  MappingName="_fechaRegistro"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                                <syncfusion:GridTextColumn HeaderText="Usuario"  MappingName="_idUsuario"
                                           AllowEditing="False"
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False"/>

                                                <syncfusion:GridTemplateColumn MappingName="Eliminar"
                                            syncfusion:FocusManagerHelper.WantsKeyInput="True" 
                                            AllowDragging="False"
                                            AllowGrouping="False" 
                                            AllowFiltering="False" 
                                            AllowSorting="False">

                                                    <syncfusion:GridTemplateColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <Button x:Name="btnEliminarRepuesto"  syncfusion:FocusManagerHelper.FocusedElement="True"  
                                                        CommandParameter="{Binding .}" Style="{StaticResource MaterialDesignFlatButton}">
                                                                <Button.Content>
                                                                    <StackPanel Orientation="Horizontal">
                                                                        <materialDesign:PackIcon Margin="0,0,0,6" Height="20" Width="auto" VerticalAlignment="Center" HorizontalAlignment="Center" Kind="CloseBox" Foreground="Black"/>
                                                                    </StackPanel>
                                                                </Button.Content>
                                                            </Button>
                                                        </DataTemplate>
                                                    </syncfusion:GridTemplateColumn.CellTemplate>
                                                </syncfusion:GridTemplateColumn>

                                            </syncfusion:SfDataGrid.Columns>
                                        </syncfusion:SfDataGrid>
                                    </syncfusion:GridViewDefinition.DataGrid>
                                </syncfusion:GridViewDefinition>
                            </syncfusion:SfDataGrid.DetailsViewDefinition>


                        </syncfusion:SfDataGrid>
                    </syncfusion:GridViewDefinition.DataGrid>
                </syncfusion:GridViewDefinition>
            </syncfusion:SfDataGrid.DetailsViewDefinition>

        </syncfusion:SfDataGrid>
Reply
Adriano Arroyo
Sep 22, 2021

The itemsourceselector property is in the cmbRepuesto column. The class with the IItemsSourceSelector interface does work. The record parameter is fine, but the datacontext parameter is null. that's the problem.

Vijayarasan Sivanandham [Syncfusion]
Sep 23, 2021

Hi Adriano Arroyo,

Thank you for using Syncfusion controls.

Based on provided information we have prepared the sample for achieve your requirement.

Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfDataGridDemo-1858677762

Please have a look at this sample and let us know if you have any concerns in this.

Regards, Vijayarasan S

Adriano Arroyo
Sep 28, 2021

Hello, thanks for answering. In this case I don't use mvvm. After several tests I found that if the sfdatagrid.itemsource is set from code, the datacontext is null in the interface. To prevent this from happening, the sfdatagrid.itemsource must be set from xaml. But now I present a new problem: I am creating a master detail sfdatagrid with crud operations, but when I add a new row (addnewrow) the combobox column does not show the list. I have checked the column from code and if it has a correct itemsource, this only happens with new rows because with existing rows the list is displayed correctly.

Reply
Vijayarasan Sivanandham [Syncfusion]
Sep 29, 2021

Hi Adriano Arroyo,

Thanks for the update.

Your requirement can be achieved by using underlying data object must be defined with default constructor. Otherwise, create instance of data object by handling the AddNewRowInitiating event. Please refer the below code snippet,

XAML Code Snippet:

<syncfusion:SfDataGrid x:Name="dataGrid"
                                       AutoGenerateColumns="False"
                                       AutoGenerateRelations="False"                                       
                                       ItemsSource="{Binding Path=OrdersDetails}"
                                       ShowGroupDropArea="True">           
            <syncfusion:SfDataGrid.DetailsViewDefinition>
                <syncfusion:GridViewDefinition RelationalColumn="OrderDetails">
                    <syncfusion:GridViewDefinition.DataGrid>
                        <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid" AddNewRowPosition="Top"
                                               AllowEditing="True"  
                                               AutoGenerateColumns="True">
                        </syncfusion:SfDataGrid>
                    </syncfusion:GridViewDefinition.DataGrid>
                </syncfusion:GridViewDefinition>
            </syncfusion:SfDataGrid.DetailsViewDefinition>
</syncfusion:SfDataGrid>

C# Code Snippet :

{
        //here defned default constructor
        public OrderDetails()
        {

        }
}

Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ModifiedSample584128272

UG Link: https://help.syncfusion.com/wpf/datagrid/data-manipulation?cs-save-lang=1&cs-lang=xaml#addnewrow-support-in-master-details-view

Please let us know, if you require further assistance on this.

Regards, Vijayarasan S

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile