RadGridView Scrolling & Paging 

PROBLEM

I have a lot of data contained in a RadGridView with scrolling and paging. I need to find a specific element in the Grid. To find it, I need to scroll down through the entire GridView and go through every page verifying the data.

 

SOLUTION

This is possible with a coded solution. To accomplish the main goal, we need to combine the solutions of the two sub-problems (Scrolling and Paging) seen here and here. We need to take the following steps to implement the solution:

  1. Change the default Find Strategy of the Grid to WhenNotVisibleReturnNull, otherwise the Find method will return a FindElementException if the element is not found.
  2. Scroll down to the bottom of the Grid.
  3. Go through the Grid page by page, repeating step two until the desired element is found.

 

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

 

C#
public FrameworkElement FindElementByTextContent(string content)
{
    int extentHeight; // The total height of the grid, visible plus non-visible     
    int rowHeight; // The height of the row
    int viewPortHeight; // The height of the visible part of the grid
    int verticalOffset = 0; // Holds the current vertical offset in the viewport
     
    // Copy the RadGridView into a local variable as a shortcut
    RadGridView grid = Pages.TelerikGridViewFor.SilverlightApp.GridViewRadgridview;
     
    // 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 extent height, view port height and the row height
    extentHeight = (int)VirtualizingPanel.GetProperty(new AutomationProperty("ExtentHeight", typeof(int)));
    rowHeight = (int)grid.GetProperty(new AutomationProperty("RowHeight", typeof(int)));
    viewPortHeight = (int)VirtualizingPanel.GetProperty(new AutomationProperty("ViewportHeight", typeof(int)));
     
    //Save the original find Find Strategy
    FindStrategy originalStrategy = grid.Find.Strategy;
     
    //Change the Find Strategy for the RadGridView
    grid.Find.Strategy = FindStrategy.WhenNotVisibleReturnNull;
     
    // Scroll through each RadGridView page until the element is found or until the "Next Page" is no longer Enabled  
    do
    {
        verticalOffset = 0;
        VirtualizingPanel.InvokeMethod("SetVerticalOffset", 0);
        while (verticalOffset < extentHeight)
        {          
                FrameworkElement fe = grid.Find.ByTextContent(content);
                if(fe != null)
                {  
                    //Return the original strategy
                    grid.Find.Strategy = originalStrategy;
                     
                    //Return the element
                    return fe;
                }
                
                verticalOffset += viewPortHeight;
                VirtualizingPanel.InvokeMethod("SetVerticalOffset", verticalOffset); 
          
        }
        Pages.TelerikGridViewFor.SilverlightApp.MoveToNextPageButtonRadbutton.User.Click(ArtOfTest.WebAii.Core.MouseClickType.LeftClick);
    }
    while(Pages.TelerikGridViewFor.SilverlightApp.MoveToNextPageButtonRadbutton.IsEnabled);
     
    //Return the original strategy
    grid.Find.Strategy = originalStrategy;
    return null;
}

 

Visual Basic
Public Function FindElementByTextContent(content As String) As FrameworkElement
    ' The total height of the grid, visible plus non-visible
    Dim extentHeight As Integer
    ' The height of the row
    Dim rowHeight As Integer
    ' The height of the visible part of the grid
    Dim viewPortHeight As Integer
    ' Holds the current vertical offset in the viewport
    Dim verticalOffset As Integer = 0
 
    ' Copy the RadGridView into a local variable as a shortcut
    Dim grid As RadGridView = Pages.TelerikGridViewFor.SilverlightApp.GridViewRadgridview
 
    ' 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 extent height, view port height and the row height
    extentHeight = CInt(VirtualizingPanel.GetProperty(New AutomationProperty("ExtentHeight", GetType(Integer))))
    rowHeight = CInt(grid.GetProperty(New AutomationProperty("RowHeight", GetType(Integer))))
    viewPortHeight = CInt(VirtualizingPanel.GetProperty(New AutomationProperty("ViewportHeight", GetType(Integer))))
 
    'Save the original find Find Strategy
    Dim originalStrategy As FindStrategy = grid.Find.Strategy
 
    'Change the Find Strategy for the RadGridView
    grid.Find.Strategy = FindStrategy.WhenNotVisibleReturnNull
 
    ' Scroll through each RadGridView page until the element is found or until the "Next Page" is no longer Enabled  
    Do
        verticalOffset = 0
        VirtualizingPanel.InvokeMethod("SetVerticalOffset", 0)
        While verticalOffset < extentHeight
            Dim fe As FrameworkElement = grid.Find.ByTextContent(content)
            If fe IsNot Nothing Then
                'Return the original strategy
                grid.Find.Strategy = originalStrategy
 
                'Return the element
                Return fe
            End If
 
            verticalOffset += viewPortHeight
 
            VirtualizingPanel.InvokeMethod("SetVerticalOffset", verticalOffset)
        End While
        Pages.TelerikGridViewFor.SilverlightApp.MoveToNextPageButtonRadbutton.User.Click(ArtOfTest.WebAii.Core.MouseClickType.LeftClick)
    Loop While Pages.TelerikGridViewFor.SilverlightApp.MoveToNextPageButtonRadbutton.IsEnabled
 
    'Return the original strategy
    grid.Find.Strategy = originalStrategy
    Return Nothing
End Function

 

Place this in the code-behind and then call this method from a coded step.