ScrollIntoView with DataPager and Gridview

4 posts, 0 answers
  1. Tim
    Tim avatar
    4 posts
    Member since:
    Aug 2010

    Posted 16 Nov 2010 Link to this post

    I've been able to programmatically set the SelectedItem in a WPF RadGridView and then call ScrollIntoView to get it to be visible in the grid.  But when I couple that with a RadDataPager it stops working.  I suspect I have to do something like Scroll in the pager, or instead of.  I have an object with a public SelectedItem that I had been passing into the gridview.ScrollIntoView.  Is there something like this I need to do to get at the right page of the DataPager, so my selected item shows up?
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 17 Nov 2010 Link to this post

    Hello Tim,

    That is completely impossible.

    Once the grid is paged, it can no longer be aware that there are other pages besides the one it is currently showing. In other words, the grid is totally clueless that there is paging going on -- when the user clicks buttons on the pager, the grid "thinks" that someone is changing it entire ItemsSource.

    Imagine that our data is 1,2,3,4 and we have a page size of 2. When we are on the first page, the grid thinks that its ItemsSource is the 1,2. When we are in the second page, the grid thinks that its ItemsSource is 3,4. So when the ItemsSource is 3,4 you simply cannot scroll to 2 since this item is not in the ItemsSource. So you can not scroll to an item, which is on another page since this item is not in the ItemsSource of the grid.

    I hope this makes sense.

    Sincerely yours,
    Ross
    the Telerik team
    See What's New in RadControls for WPF in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Tim
    Tim avatar
    4 posts
    Member since:
    Aug 2010

    Posted 17 Nov 2010 Link to this post

    Obviously I can do this myself, by knowing what item is selected, dividing by the number items per page, then telling the DataPager to go to that page.  I was just hoping that since this seems like quite a common need for developers you might have incorporated such a technique into the $1000 package we just bought. If you have any other ideas please let me know, thanks.
  5. Lawrence
    Lawrence avatar
    10 posts
    Member since:
    Jul 2013

    Posted 10 Feb 2015 Link to this post

    If you have all of the data, you can use this extension method.
    public void ScrollIntoView<T>(T dataItem, RadDataPager radDataPager)
    {
        var items = (Collection<T>)this.ItemsSource;
        int index = items.IndexOf(dataItem);
        int page = (int)Math.Floor((double)index / radDataPager.PageSize);
     
        radDataPager.MoveToPage(page);
     
        this.SelectedItem = items[index];
        this.ScrollIntoView(this.SelectedItem);
    }
Back to Top