How to Scroll to top of Radgridview after Sorting

1 Answer 406 Views
GridView ScrollBar
Sean
Top achievements
Rank 1
Iron
Sean asked on 31 Aug 2022, 04:01 PM
Whenever a sort filter is applied to my radgridview, it holds the selectedItem and scrolls to it further down in the list.  How to I programatically scroll to the top or disable this from jumping to the selected item? (Sorting is pre-set in the UI component, not programmatic)

Thanks!
Curtis
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 01 Sep 2022, 08:03 PM

Hello Sean!

I've been using Telerik for years now and have a boatload of "Helper" methods i use for all variety of issues.  One of these might help you - I write in VB.net but it shouldn't take more than a moment to convert to C# if that's your bag:

 

Public Sub SelectRow(ByVal pGrid As RadGridView, ByVal pRow As GridViewRowInfo)
If IsNothing(pGrid) Then Exit Sub
If IsNothing(pRow) Then Exit Sub

pGrid.CurrentRow = pRow
pRow.IsSelected = True
pRow.IsCurrent = True
pRow.EnsureVisible()
End Sub

Public Sub SelectFirstRow(ByVal pGrid As RadGridView)
If IsNothing(pGrid) Then Exit Sub

If pGrid.Rows.Count > 0 Then
pGrid.CurrentRow = pGrid.Rows(0)
pGrid.Rows(0).IsSelected = True
pGrid.Rows(0).IsCurrent = True
pGrid.Rows(0).EnsureVisible()
End If
End Sub

 

The first method you pass in the Grid and the Row and it'll select, highlight and make sure it's visible.

The second method does that same except it'll be the first row.  For *MY* application there was no need to test to see if the first row was visible or not so if that's possible for your ap, you'll want another method to crawl the row collection to find the first visible row and use that index instead of 0 like I do above.

 

Hope this helps!

Cheers.

Sean
Top achievements
Rank 1
Iron
commented on 02 Sep 2022, 12:27 PM

Curtis, Thanks for those helpers!

1 Answer, 1 is accepted

Sort by
1
Maria
Telerik team
answered on 02 Sep 2022, 11:25 AM

Hi Sean,

Thank you for writing!

When you apply the sorting filter in the radGridView the scrollbar doesn't change its position by default. To programmatically scroll to the top row you can try the SortChanged event in the radGridView and use the ScrollToRow function. 

private void RadGridView1_SortChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    radGridView1.TableElement.ScrollToRow(0);
}

You can see more information in the documentation below:

https://docs.telerik.com/devtools/winforms/controls/gridview/scrolling/scrolling-programmatically

https://docs.telerik.com/devtools/winforms/controls/gridview/sorting/events

Try it and don't hesitate to write again if this information is not about what you try to achieve.

Regards,
Maria
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView ScrollBar
Asked by
Sean
Top achievements
Rank 1
Iron
Answers by
Maria
Telerik team
Share this question
or