Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# UI DataGrid Automation
Hi,
I'm writing a UI automation software. I need to select a row in the datagrid and then click on the run button. I tried most of the example codes in the internet and they didn't work for me. For example for selecting a gridview row:
 
When I write the following code:
AutomationElement dataGrid = this.mainWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "2885"));
 
if (dataGrid != null) {
 
GridPattern pattern = GetGridPattern(dataGrid);
AutomationElement tempElement = pattern.GetItem(1, 1);
tempElement.SetFocus();
}
 

I receive the error: "Target element cannot receive focus." which is related to the last line.
 
I also tried the code:
AutomationElement mainGrid = // find the grid in the window var columnCount = (int)mainGrid.GetCurrentPropertyValue(GridPattern.ColumnCountProperty);

var mainGridPattern = (GridPattern)mainGrid.GetCurrentPattern(GridPattern.Pattern);
 
var rowToSelect = 2;
 
// select just the first cell var item = mainGridPattern.GetItem(rowToSelect, 0);

var itemPattern = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern);
 
itemPattern.Select();
 
but I and I received the error :"Unsupported Pattern".
 
I should mention that I'm using UI Spy for retrieving the elements properties.
 
Could you explain me what's wrong and how should I select a row?
Posted 10-Jul-13 10:00am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Been a while, but check this out
 
Quote:
GridPattern does not support active manipulation of a grid; the TransformPattern control pattern is required for this functionality.

 
http://msdn.microsoft.com/en-us/library/system.windows.automation.gridpattern.aspx[^]
  Permalink  
Comments
kasra515 at 11-Jul-13 10:20am
   
Thank you for your answer. The Transform control "Represents a control that can be moved, resized, or rotated within a two-dimensional space.", so it's not what I'm looking for. Can anyone help me to get it work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 275
1 Maciej Los 230
2 Sergey Alexandrovich Kryukov 185
3 Afzaal Ahmad Zeeshan 152
4 ProgramFOX 130
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 10 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100