How do I – Scroll the Telerik RadGridView to the top after a data refresh?

Written by Cornelius J. van Dyk on . Posted in How Do I...

What seems like a very simple thing to do, and quite frankly it should be, turned out to be a little more tricky than I thought. Setting the Scene The app has a RadGridView control that displays information from a SQL database. The data is presented to the control via a simple Linq2SQL DataSet. Given that the RadGridView control has a .CurrentRow property which is updatable, logic dictates that a simple statement such as this:

 1:  grdContentSources.CurrentRow = grdContentSources.Rows[0];
would take care of moving the cursor to the top after the data refresh. Alas, it doesn’t. Next I experimented with disconnecting the data source, filling the table adapter and then reconnecting the data source to the grid, but that didn’t work either. Internet searches, especially the very helpful Telerik support site, indicated that we should be using the .IsCurrent() method of the row instead. So this should solve the problem, right?
 1:  grdContentSources.Rows[0].IsCurrent = true;
Unfortunately, that didn’t solve the problem either. Then I found an obscure comment from “Jack”, and Admin on the Telerik team, that pointed me towards the .ChildRows collection for sorted data instead. Since I did have some sorting on the grid, I tried it and it worked! Here’s the complete code:
 1:  Cursor.Current = Cursors.WaitCursor;
   2:  this.crawlManDBDataSet.vwContentSourceCrawlHistory.Clear();
   3:  this.vwContentSourceCrawlHistoryTableAdapter.Fill(
   4:    this.crawlManDBDataSet.vwContentSourceCrawlHistory);
   5:  grdContentSources.ChildRows[0].IsCurrent = true;
   6:  Cursor.Current = Cursors.Default;
Since the operation takes some time, in line 1 we set the cursor to the hour glass. In line 2 we clear the source table for the grid. In line 3 we refill the table to get refreshed data. This reflects back onto the grid as it triggers a .Changed() event to refresh the grid view. Line 5 is where the magic happens as we simply set the .IsCurrent property of the .ChildRows[0] row to be true. Using zero takes the grid back to the top. We could set this to any value within the range of the grid e.g. we could have used:
 1:  grdContentSources.ChildRows[
   2:    grdContentSources.ChildRows.Count - 1].IsCurrent = true;
in order to scroll the grid to the bottom instead.



Tags: , , ,

Trackback from your site.

Cornelius J. van Dyk

Born and raised in South Africa during the 70's I got my start in computers when a game on my Sinclair ZX Spectrum crashed, revealing it's BASIC source code. The ZX had a whopping 48K of memory which was considered to be a lot in the Commodore Vic20 era, but more importantly, it had BASIC built into the soft touch keyboard. Teaching myself to program, I coded my first commercial program at age 15.

After graduating high school at 17, I joined the South African Air Force, graduating the Academy and becoming a Pilot with the rank of First Lieutenant by age 20. After serving my country for six years, I made my way back into computer software.

Continuing my education, I graduated Suma Cum Laude from the Computer Training Institute before joining First National Bank where my work won the Smithsonian Award for Technological Innovation in the field of Banking and Insurance. Soon I met Will Coleman from Amdahl SA, who introduced me to a little known programming language named Huron/ObjectStar. As fate would have it, this unknown language and Y2K brought me to the USA in 1998.

I got involved with SharePoint after playing around with the Beta for SharePoint Portal Server 2003. Leaving my career at Rexnord to become a consultant in 2004, I was first awarded the Microsoft Most Valuable Professional Award for SharePoint in 2005, becoming only the 9th MVP for WSS at the time. I fulfilled a life long dream by pledging allegiance to the Flag as a US citizen in 2006. I met the love of my life and became a private consultant in 2008. I was honored to receive my ninth MVP award for SharePoint Server in 2013.

Leave a comment

You must be logged in to post a comment.