RadGridView Scrolling

Note: This issue can now be resolved without code.

PROBLEM

I need to scroll down through the entire GridView verifying the data, because currently only the rows presented in the Visual Tree can be tested.

SOLUTION

This is a limitation of the Visual Tree contained in the Silverlight engine. Silverlight puts into the Visual Tree only the UI components necessary to render the page on the screen.

 

Here is an example. Let's say you have a grid that holds 100 rows of data but can only display 20 at a time. Silverlight typically will only put 22 rows of UI elements in the Visual Tree (20 visible plus one above and one below). The only way to get all 100 rows is to scroll down through the entire grid. 

 

The following code demonstrates how to accomplished this on a Telerik demo page.

 

C#

int verticalOffset = 0; // Holds the current vertical offset in the viewport
int viewPortHeight; // The height of the visible part of the grid
int extentHeight; // The total height of the grid, visible plus non-visible

 

// Copy the RadGridView into a local variable as a shortcut
RadGridView grid = Pages.TelerikGridForSilverlight.SilverlightApp.RadGridView1Radgridview0;

 

// Grab the VirtualizingPanel contained in the RadGridView. This is used to control the viewable portion of the grid.
FrameworkElement VirtualizingPanel = grid.Find.ByType("GridViewVirtualizingPanel");

 

// Detect the view port height and the extent height
viewPortHeight = (int)VirtualizingPanel.GetProperty(new AutomationProperty("ViewportHeight", typeof(int)));
extentHeight = (int)VirtualizingPanel.GetProperty(new AutomationProperty("ExtentHeight", typeof(int)));

 

// Make sure it is scrolled to the very top
// Walk through the entire grid verifying the data
VirtualizingPanel.InvokeMethod("SetVerticalOffset", 0);
while (verticalOffset < extentHeight)
{
foreach (GridViewRow r in grid.Rows)
{
Assert.AreEqual<string>(Data["State"].ToString(), r.Cells[7].Text.Substring(0,4));

}

// Scroll down one page
verticalOffset += viewPortHeight;
VirtualizingPanel.InvokeMethod("SetVerticalOffset", verticalOffset);
}

 

Visual Basic

' Holds the current vertical offset in the viewport
Dim verticalOffset As Integer = 0
' The height of the visible part of the grid
Dim viewPortHeight As Integer
' The total height of the grid, visible plus non-visible
Dim extentHeight As Integer

 

' Copy the RadGridView into a local variable as a shortcut
Dim grid As RadGridView = Pages.TelerikGridForSilverlight.SilverlightApp.RadGridView1Radgridview0

 

' Grab the VirtualizingPanel contained in the RadGridView. This is used to control the viewable portion of the grid.
Dim VirtualizingPanel As FrameworkElement = grid.Find.ByType("GridViewVirtualizingPanel")

 

' Detect the view port height and the extent height
viewPortHeight = CInt(VirtualizingPanel.GetProperty(New AutomationProperty("ViewportHeight", GetType(Integer))))
extentHeight = CInt(VirtualizingPanel.GetProperty(New AutomationProperty("ExtentHeight", GetType(Integer))))

 

' Make sure it is scrolled to the very top
' Walk through the entire grid verifying the data
VirtualizingPanel.InvokeMethod("SetVerticalOffset", 0)
While verticalOffset < extentHeight
For Each r As GridViewRow In grid.Rows
Assert.AreEqual(Of String)(Data("State").ToString(), r.Cells(7).Text.Substring(0, 4))
Next

 

' Scroll down one page
verticalOffset += viewPortHeight
VirtualizingPanel.InvokeMethod("SetVerticalOffset", verticalOffset)
End While

 

Due to the volume of data contained in the sample RadGridView, this test will take hours to run through it all. But it shows all the necessary steps.