New to Telerik Test Studio Dev EditionStart a free 30-day trial

RadGridView Scrolling

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 accomplish this against 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.TelerikGridViewFor.SilverlightApp.RadGridView1Radgridview;
	// 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)));

	Dictionary<string, int> rowsHash = new Dictionary<string, int>();
	// Make sure it is scrolled to the very top
	// Walk through the entire grid verifying the data
	VirtualizingPanel.InvokeMethod("SetVerticalOffset", 0);
	int rowNum = 0;
	while (verticalOffset < extentHeight)
	{
	grid.Refresh();
		foreach (GridViewRow r in grid.Rows)
		{
		string rowId = r.Cells[2].Text;
			if (!rowsHash.Keys.Contains(rowId))
				{
					rowsHash.Add(rowId, rowNum++);
					Console.WriteLine(r.Cells[0].Text + ", " + r.Cells[1].Text + ", " + r.Cells[2].Text + ", " + r.Cells[3].Text + ", " + r.Cells[4].Text + ", " + r.Cells[5].Text + ", " + r.Cells[6].Text);
				}
		}
	// Scroll down one page
	verticalOffset += viewPortHeight;
	VirtualizingPanel.InvokeMethod("SetVerticalOffset", verticalOffset);
	}

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.

In this article
Solution
Not finding the help you need?
Contact Support