Getting DataGrid.ScollIntoView to position row at top of view area

Posted by: elogsdon on 7 September 2018, 12:33 am EST

    • Post Options:
    • Link

    Posted 7 September 2018, 12:33 am EST

    I am using the Datagrid.ScrollIntoView(row, col) to programmatically scroll a row into the viewable area of my data grid. The row is positioned at the bottom of the viewable area. Is there any way to make this the top row?

    Eric

  • Posted 9 September 2018, 3:01 pm EST

    Hello Eric,

    For scrolling a row into view and also setting it as the top/first visible row, you need to use C1DataGrid’s ViewPort.ScrollToVerticalOffset method and pass in row*Datagrid.RowHeight.Value as argument, as follows:

     _grid.Viewport.ScrollToVerticalOffset(row*_grid.RowHeight.Value);
    

    Attached is a sample application for your reference.

    Thanks,

    Ruchir

    FirstVisibleRow_C1DataGrid.zip

  • Posted 10 September 2018, 7:13 am EST

    Thanks. Works like a charm.

    Eric.

  • Posted 11 September 2018, 5:48 am EST

    I may have spoken too soon. If a master has details visible (expanded) and I programmatically select a master row further down and do a scroll to vertical offset, the row I selected is offset down by the number of detail rows that are visible above. This is account/bill lookup. If account A has three details visible and I search for and position to account B it shows up on the 4th line of the grid instead of the first. If Account A isn’t showing it’s details, Account B is positioned on the first line as I would expect.

    I tried to programmatically collapse the details in Account A by setting RowVisibility to collapsed on the master row. Visibly this works, but Account B is still offset by three rows.

    Am I looking in the right place to reset the grid so the masters are collapsed (not showing details)?

    Thanks, Eric.

  • Posted 11 September 2018, 8:41 pm EST - Updated 4 October 2022, 12:14 am EST

    Hello Eric,

    ScrollToVerticalOffset method is used to scroll the grid contents to a specified position, which is calculated by the number of rows currently visible in grid. Therefore, C1DataGrid’s ScrollToVerticalOffset considers the number of visible rows.

    If the child rows of nodes(master rows) above ‘Our Row’ are visible, then the vertical position of ‘Our Row’ in grid would change (increase)(since the visible rows changed). So, the behavior you are observing is by-design.

    Now, if you want that the grid should always scroll to ‘Our Row’ irrespective of whether the child rows above are expanded/collapsed, this needs custom implementation. For this, I devised the following approach:

    int ourRow = 15;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int visRowCount = 0;
    
        foreach (C1.WPF.DataGrid.DataGridRow rw in gridCategories.Rows)
        {
            if( rw.DetailsPresenter !=null && rw.DetailsVisibility == Visibility.Visible && rw.VerticalOffset< (ourRow + visRowCount) * gridCategories.RowHeight.Value)
            {
                visRowCount += ((rw.DetailsPresenter as DataGridDetailsPresenter).Content as C1DataGrid).Rows.Count;
            }
        }
        gridCategories.Viewport.ScrollToVerticalOffset((ourRow + visRowCount) * gridCategories.RowHeight.Value);
    }
    

    Conceptually, above I am trying to see which rows are master rows and whose children are visible and keeping track of their children. Also, I am checking whether the row is above ‘Our Row’/not. If is not above, then we need not consider it since, it would not affect the vertical position of ‘Our Row’. Could you please check whether the same works at your end.

    Note: ‘Our Row’ is the row we want to show at top.

    Regards,

    Ruchir

    ScrollToVerticalOffset.zip

  • Posted 12 September 2018, 12:03 am EST

    Thanks Ruchir, I thought something like that might be the case. I will work with this today and tomorrow and let you know how it works.

    Eric

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels