New to Telerik Test Studio Dev Edition? Start a free 30-day trial
RadGridView Verify Data in a Specific Column
I need to find the correct column of a GridView based on something other than index, such as text content or a data driven variable.
Solution
Here is how to accomplish this against a Telerik demo site. First you need to retrieve all the Header Row Cells from the Grid and iterate through each one looking for a specific string. Each time it isn't found, the integer idx is increased by one. Once a match is made, idx will equal the index of the target column. Then you can use that integer to identify the column later:
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.TelerikGridViewFor1.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)));
VirtualizingPanel.InvokeMethod("SetVerticalOffset", 0);
//get the Header Row Cells and iterate through them
IList<GridViewHeaderCell> headers = grid.HeaderRow.HeaderCells;
int idx = 0;
foreach(GridViewHeaderCell header in headers)
{
//Instead of a sting (here it's "Unit Price"), you can use a variable
if(header.Text.Equals("Unit Price", StringComparison.CurrentCultureIgnoreCase))
{
break;
}
idx++;
}
Assert.IsTrue(idx < headers.Count, string.Format("Column {0} not found", "Unit Price"));
// Walk through the entire grid verifying the data
while (verticalOffset < extentHeight)
{
foreach (GridViewRow r in grid.Rows)
{
//Here the integer "idx" is used in place of the actual index number
Assert.AreEqual<string>(Data["State"].ToString(), r.Cells[idx].Text.Substring(0,1));
}
// Scroll down one page
verticalOffset += viewPortHeight;
VirtualizingPanel.InvokeMethod("SetVerticalOffset", verticalOffset);
}