Filter ComboBoxColumn itemssource based on selection from other column

0 Answers 13 Views
GridView
hhgm
Top achievements
Rank 1
hhgm asked on 22 May 2022, 12:52 AM

Hi

I have a DataGrid where the XAML is:

                            <telerik:RadGridView Grid.Column="2" 
                                                 NewRowPosition="Bottom"
                                                 GroupRenderMode="Flat"
                                                 ShowGroupPanel="False"
                                                 AutoGenerateColumns="False"
                                                 SelectedItem="{Binding SelectedDevice.SelectedDataConfiguration, Mode=TwoWay}"
                                                 ItemsSource="{Binding SelectedDevice.DataConfiguration}"
                                                 IsSynchronizedWithCurrentItem="True"
                                                 x:Name="DataConfigurationGrid"
                                                 Margin="2">
                                <telerik:RadGridView.Columns>
                                    <telerik:GridViewComboBoxColumn Header="Data page" DataMemberBinding="{Binding MethodId, Mode=TwoWay}" ItemsSourceBinding="{Binding DataContext.DataDefinitions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SatelliteManager}}}" DisplayMemberPath="Name"/>
                                    <telerik:GridViewComboBoxColumn Header="Class" DataMemberBinding="{Binding ClassId, Mode=TwoWay}" ItemsSource="{Binding SelectedDevice.SelectedTournament.Classes}" IsReadOnly="{Binding SelectedDevice.SelectedDataConfiguration.MethodId.CanFilterClass, Converter={StaticResource InvertedBooleanConverter}}" IsEnabled="{Binding SelectedDevice.SelectedDataConfiguration.MethodId.CanFilterClass}" DisplayMemberPath="Name" SelectedValueMemberPath="Id"/>
                                    <telerik:GridViewComboBoxColumn Header="Group" 
                                                                    DataMemberBinding="{Binding GroupId, Mode=TwoWay}" 
                                                                    IsReadOnly="{Binding SelectedDevice.SelectedDataConfiguration.MethodId.CanFilterGroup, Converter={StaticResource InvertedBooleanConverter}}" 
                                                                    IsEnabled="{Binding SelectedDevice.SelectedDataConfiguration.MethodId.CanFilterGroup}" 
                                                                    DisplayMemberPath="DescriptiveName" 
                                                                    SelectedValueMemberPath="Id">
                                        <telerik:GridViewComboBoxColumn.ItemsSource>
                                            <MultiBinding>
                                                <MultiBinding.Converter>
                                                    <converters:GroupsByDivisionConverter/>
                                                </MultiBinding.Converter>
                                                <Binding Path="SelectedDevice.SelectedTournament.Groups"/>
                                                <Binding Path="SelectedDevice.SelectedDataConfiguration.ClassId"/>
                                            </MultiBinding>
                                        </telerik:GridViewComboBoxColumn.ItemsSource>
                                    </telerik:GridViewComboBoxColumn>
                                    <telerik:GridViewComboBoxColumn Header="Pitch" DataMemberBinding="{Binding PitchId, Mode=TwoWay}" ItemsSource="{Binding SelectedDevice.SelectedTournament.Pitches}" IsReadOnly="{Binding SelectedDevice.SelectedDataConfiguration.MethodId.CanFilterPitch, Converter={StaticResource InvertedBooleanConverter}}" IsEnabled="{Binding SelectedDevice.SelectedDataConfiguration.MethodId.CanFilterPitch}" DisplayMemberPath="Name" SelectedValueMemberPath="Id"/>
                                    <telerik:GridViewDataColumn Header="Duration">
                                        <telerik:GridViewDataColumn.CellTemplate>
                                            <DataTemplate>
                                                <telerik:RadNumericUpDown Minimum="1" Maximum="1000" IsInteger="True" Width="50" Margin="2" Value="{Binding Duration, Mode=TwoWay}"/>
                                            </DataTemplate>
                                        </telerik:GridViewDataColumn.CellTemplate>
                                    </telerik:GridViewDataColumn>
                                </telerik:RadGridView.Columns>
                            </telerik:RadGridView>

 

I have a Wrapper class I use for the itemssource for the comboboxes:

    public class TournamentSatelliteWrapper
    {
        public TournamentInfo Tournament { get; set; }
        public List<ClassInfo> Classes { get; set; }
        public List<GroupInfo> Groups { get; set; }
        public List<PitchInfo> Pitches { get; set; }
    }

Now, when I select a method where I can filter by division and group, these initial lists are filled correctly. But the list of groups are all groups..across divisions. So, when I select division (class), I have created a converter to alter the list of the groups:

    public class GroupsByDivisionConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Guid? classId = values[1] == DependencyProperty.UnsetValue ? null : values[1] as Guid?;

            if (values[0] is List<GroupInfo>)
            {
                List<GroupInfo> groups = (List<GroupInfo>)values[0];

                if (classId.HasValue)
                    groups = groups.Where(x => x.Class.Id == classId.Value).ToList();

                return groups;
            }

            // fallback
            return values[0];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

All this works fine... except that when I switch between the different rows, I only see the name of the selected group for the selected row. I can see the issue, I am just not sure how to get around this. 

I can understand that the itemssource list does not contain the referenced item of the selected group when looking at a row where a different division is selected.. when applying the converter... the resulting list is the source for all the cells (each row). How could I only alter the list for the selected row? and the other cells keep their altered lists accordingly to have the selected group name display correctly?

hhgm
Top achievements
Rank 1
commented on 22 May 2022, 01:47 AM

So, after a bit of thought... I realize I did not add the class definition for the list behind the gridview... but I have now added a property to that class to actually hold the current list of groups for that item, and my thought now is to add a combobox to a plain gridview column to bind to that property... 
Krasimir Balchev
Telerik team
commented on 26 May 2022, 12:10 PM

Hello Hans-Henrik,

I was unable to fully use your code because some sections were missing. However, I have attached a project which allows you to change the ItemsSource of a cell from a ComboBoxColumn through selection from another ComboBoxColumn.

If it achieves your desired result, hopefully you can use it as a reference to implement this functionality in your own project.

Regards,
Krasimir Balchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

No answers yet. Maybe you can help?

Tags
GridView
Asked by
hhgm
Top achievements
Rank 1
Share this question
or