How can you hide row details by clicking an already selected row?

2 Answers 135 Views
GridView
Tom
Top achievements
Rank 1
Iron
Tom asked on 09 Mar 2022, 03:20 AM

Hi there,

I am looking for a way to programmatically deselect the current selected row by clicking on it. I do not wish to use the ToggleRowDetailsColumn. 

Here is the logic I wish to implement:

if (clickedItem == this.gridView.SelectedItem)
{
    this.gridView.SelectedItems.Clear();
}
else
{
    this.gridView.SelectedItem = clickedItem;
}

Thanks

2 Answers, 1 is accepted

Sort by
1
Accepted
Martin Ivanov
Telerik team
answered on 09 Mar 2022, 07:13 AM | edited on 09 Mar 2022, 07:16 AM

Thanks for sharing the solution, Tom.

An alternative one would be to use the VisibleWhenSelected RowDetailsVisibilityMode. This way when you click onto a row to select it, the row details will expand, and when you deselect it they will hide. This can be used in combination with the Multiple SelectionMode which will allow you to select a row on first click and deselect it on the second click.

<telerik:RadGridView SelectionMode="Multiple" RowDetailsVisibilityMode="VisibleWhenSelected"/>

0
Tom
Top achievements
Rank 1
Iron
answered on 09 Mar 2022, 05:29 AM

I have since found the answer


    public partial class View1 : UserControl
    {
        public View1()
        {
            InitializeComponent();
            this.gridView.AddHandler(GridViewCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnGridMouseLeftButtonDown), true);
        }

        private void OnGridMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var senderElement = (FrameworkElement) e.OriginalSource;
            var clickedRow = senderElement.ParentOfType<GridViewRow>();

            if (clickedRow != null)
            {
                clickedRow.DetailsVisibility = clickedRow.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
            }
        }
    }

Tags
GridView
Asked by
Tom
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Tom
Top achievements
Rank 1
Iron
Share this question
or