This is a migrated thread and some comments may be shown as answers.

Hierarchical GridView auto select option so the ParentRow is selected when a child is selected

5 Answers 81 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Cale
Top achievements
Rank 1
Cale asked on 10 May 2019, 05:22 PM

Currently when you highlight a row in the child GridView, the ParentRow becomes highlighted. It would be really nice to have an option so that the ParentRow would be selected when a row in the child GridView is selected. This should work recursively selecting the ParentRow all the way up the hierarchy.

I've written a workaround, but it isn't pretty.

Please add this functionality. :)

 

My case involves a three tier hierarchical GridView. If I select a row in a third tier GridView, then I want its ParentRow to become selected on the second tier and the second tier's ParentRow to become selected on the first tier. Hopefully that makes sense.

5 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 15 May 2019, 11:08 AM
Hi Cale,

Indeed this behavior is not supported out of the box. If you think that this is a good-to-have feature you can log a feature request in our Feedback Portal. This way our development team will review it and consider its implementation.

As a possible workaround is to subscribe to the SelectionChanged child grid and manually select the parent row. To set properties of the child grid you can check the How-to: Set properties to the child gridview help article in our documentation.

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Cale
Top achievements
Rank 1
answered on 15 May 2019, 03:06 PM

Thanks! I didn't realize that there was a different forum for feature requests. I went ahead and added it there.

https://feedback.telerik.com/wpf/1409213-hierarchical-gridview-auto-select-option-so-the-parentrow-is-selected-when-a-child-is-selected

0
Usman
Top achievements
Rank 1
answered on 27 Jun 2019, 01:48 PM

Hi Cale and Dinko,

Can you please suggest how to achieve this behaviour. A possible code snippet would help too.

0
Cale
Top achievements
Rank 1
answered on 27 Jun 2019, 05:52 PM

There are probably a few different ways to do this.

In my case I also wanted the selection of a parent to deselect the children, so you might need to massage the code a bit if you don't want that functionality.

Here is the basic code that I wrote:

namespace MyNamespace {
    public class RadGridViewParentAndChildSelectionSyncer {
        private RadGridView _rootGridView;
        private RadGridView _lastSelectedSecondGridView;
        private RadGridView _lastSelectedThirdGridView;
        private bool _selectingParents;
 
        public void RootGridViewSelectionChanged(object sender, SelectionChangeEventArgs e) {
            e.Handled = true;
            if (e.AddedItems.Any()) {
                _rootGridView = e.Source as RadGridView;
                if (!_selectingParents) {
                    _lastSelectedSecondGridView?.UnselectAll();
                    _lastSelectedSecondGridView = null;
                    _lastSelectedThirdGridView?.UnselectAll();
                    _lastSelectedThirdGridView = null;
                    SelectParentRow(_rootGridView);
                }
            }
        }
 
        public void SecondGridViewSelectionChanged(object sender, SelectionChangeEventArgs e) {
            e.Handled = true;
            if (e.AddedItems.Any()) {
                _lastSelectedSecondGridView = e.Source as RadGridView;
                if (!_selectingParents) {
                    _rootGridView?.UnselectAll();
                    _lastSelectedThirdGridView?.UnselectAll();
                    _lastSelectedThirdGridView = null;
                    SelectParentRow(_lastSelectedSecondGridView);
                }
            }
        }
 
        public void ThirdGridViewSelectionChanged(object sender, SelectionChangeEventArgs e) {
            e.Handled = true;
            if (e.AddedItems.Any()) {
                if (!_selectingParents) {
                    _lastSelectedThirdGridView = e.Source as RadGridView;
                    _rootGridView?.UnselectAll();
                    _lastSelectedSecondGridView?.UnselectAll();
                    _lastSelectedSecondGridView = null;
                    SelectParentRow(_lastSelectedThirdGridView);
                }
            }
        }
 
        internal void OnMouseButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            if (!_selectingParents) {
                var radGridView = (e.OriginalSource as DependencyObject).GetVisualParent<RadGridView>();
                if (radGridView == _rootGridView) {
                    _lastSelectedSecondGridView?.UnselectAll();
                    _lastSelectedSecondGridView = null;
                    _lastSelectedThirdGridView?.UnselectAll();
                    _lastSelectedThirdGridView = null;
                }
                if (radGridView == _lastSelectedSecondGridView) {
                    _lastSelectedThirdGridView?.UnselectAll();
                    _lastSelectedThirdGridView = null;
                }
            }
        }
 
        private void SelectParentRow(RadGridView radGridView) {
            _selectingParents = true;
            var parentRow = radGridView.ParentRow;
            if (parentRow != null) {
                var radGridViewParent = parentRow.GetVisualParent<RadGridView>();
                radGridViewParent.SelectedItem = parentRow.Item;
                SelectParentRow(radGridViewParent);
            }
            _selectingParents = false;
        }
 
    }
}

Here is an example of the View.xaml.cs:

using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
 
namespace MyNamespace {
    public partial class ViewWithRadGridView {
        private RadGridViewParentAndChildSelectionSyncer _selectionSyncer = new RadGridViewParentAndChildSelectionSyncer();
        public RouteStatusView() {
            InitializeComponent();
        }
 
        private void FirstGridSelectionChanged(object sender, SelectionChangeEventArgs e) {
            _selectionSyncer.RootGridViewSelectionChanged(sender, e);
        }
 
        private void SecondGridSelectionChanged(object sender, SelectionChangeEventArgs e) {
            _selectionSyncer.SecondGridViewSelectionChanged(sender, e);
        }
 
        private void ThirdGridSelectionChanged(object sender, SelectionChangeEventArgs e) {
            _selectionSyncer.ThirdGridViewSelectionChanged(sender, e);
        }
 
        private void OnMouseButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            _selectionSyncer.OnMouseButtonUp(sender, e);
        }
    }
}

Here is an example of the View.xaml :

<UserControl x:Class="QRouterView.Views.TablesVersion.RouteStatusView"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:System="clr-namespace:System;assembly=mscorlib"
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <telerik:RadGridView Name="FirstGridView" Grid.Row="1" ItemsSource="{Binding ItemsSource}" IsReadOnly="True" AutoGenerateColumns="False" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" SelectedItem="{Binding SelectedItem}" SelectionChanged="FirstGridSelectionChanged" PreviewMouseUp="OnMouseButtonUp" EnableLostFocusSelectedState="False" >
            <telerik:RadGridView.Columns>
            </telerik:RadGridView.Columns>
            <telerik:RadGridView.ChildTableDefinitions>
                <telerik:GridViewTableDefinition/>
            </telerik:RadGridView.ChildTableDefinitions>
            <telerik:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <telerik:RadGridView x:Name="SecondGridView" ItemsSource="{Binding ItemsSource}" IsReadOnly="True" AutoGenerateColumns="False" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" SelectedItem="{Binding SelectedItem}" SelectionChanged="SecondGridSelectionChanged" PreviewMouseUp="OnMouseButtonUp" EnableLostFocusSelectedState="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
                        <telerik:RadGridView.Columns>
                        </telerik:RadGridView.Columns>
                        <telerik:RadGridView.ChildTableDefinitions>
                            <telerik:GridViewTableDefinition/>
                        </telerik:RadGridView.ChildTableDefinitions>
                        <telerik:RadGridView.HierarchyChildTemplate>
                            <DataTemplate>
                                <telerik:RadGridView Name="ThirdGridView" ItemsSource="{Binding ItemsSource}" IsReadOnly="True" AutoGenerateColumns="False" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" SelectedItem="{Binding SelectedItem}" SelectionChanged="ThirdGridSelectionChanged" EnableLostFocusSelectedState="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
                                    <telerik:RadGridView.Columns>
                                    </telerik:RadGridView.Columns>
                                </telerik:RadGridView>
                            </DataTemplate>
                        </telerik:RadGridView.HierarchyChildTemplate>
                  </telerik:RadGridView>
                </DataTemplate>
            </telerik:RadGridView.HierarchyChildTemplate>
        </telerik:RadGridView>
    </Grid>
</UserControl>
0
Usman
Top achievements
Rank 1
answered on 28 Jun 2019, 11:24 AM

Thanks Cale,

As our requirements were not too complex, I used following behaviour to achieve this functionality. I use this behaviour in child grids.

public class ChildGridRowSelectionToParentRowSelectionBehavior : Behavior<RadGridView>
    {
 
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.GotFocus += AssociatedObject_GotFocus;
        }
 
        private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
        {
            if( this.AssociatedObject.ParentRow != null)
            {
                var parentRow = this.AssociatedObject.ParentRow;
                if (parentRow.DataContext != null)
                {
                    var grid = parentRow.ParentOfType<RadGridView>();
                    if (grid != null)
                    {
                        grid.SelectedItem = parentRow.DataContext;
                    }
                }
            }
        }
         
        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
        }
    }
Tags
GridView
Asked by
Cale
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Cale
Top achievements
Rank 1
Usman
Top achievements
Rank 1
Share this question
or