New to Telerik UI for WPFStart a free 30-day trial

How to Keep the Selected Item in the Viewport when the Collection is Changed

Updated on Sep 15, 2025

Environment

ProductRadGridView for WPF

Description

When an item is added or removed from the source collection of the RadGridView control, the scroll position is changed.

This article explains how to keep the selected item in the viewport when the collection is changed.

Solution

To keep the position of the selected item, you can handle the CollectionChanged event of its Items collection and offset the GridViewScrollViewer by the amount of items added/removed multiplied by the RowHeight.

Handle CollectionChanged event to keep item in viewport

C#
    gridView.Items.CollectionChanged += (s, a) =>
    {
        var selectedItem = gridView.SelectedItem;
        var selectedItemIndex = gridView.Items.IndexOf(selectedItem);

        double offset = 0;

        if (a.NewItems != null)
        {
            foreach (var item in a.NewItems)
            {
                if (gridView.Items.IndexOf(item) < selectedItemIndex)
                {
                    offset += gridView.RowHeight;
                }
            }
        }

        if (a.OldItems != null)
        {
            foreach (var item in a.OldItems)
            {
                if (a.OldStartingIndex < selectedItemIndex)
                {
                    offset -= gridView.RowHeight;
                }
            }
        }

        var scrollViewer = gridView.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault();
        scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset);
        scrollViewer.UpdateLayout(); // required so that the value of the VerticalOffset property is updated
    };

A working example of this approach can be found in our SDK Samples Browser. It is titled Preserve Selected Item Scroll Position.

See Also