08 August 2013

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

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.


Enjoy
C


image

No comments:

Post a Comment

Comments are moderated only for the purpose of keeping pesky spammers at bay.

SharePoint Remote Event Receivers are DEAD!!!

 Well, the time has finally come.  It was evident when Microsoft started pushing everyone to WebHooks, but this FAQ and related announcement...