The following sample demonstrates how to read and write cell elements of a grid view.
This code works with the Microsoft DataGridView Control sample.
You can download the sample application from:
http://msdn2.microsoft.com/en-us/library/1x64c23x.aspx
Code: click into code to enlarge
class Program
{
static public void DumpElementTree(Element element, int level)
{
level++;
int childCount = element.GetChildCount;
for (int index = 0; index < childCount && index < 10; index++)
{
Element child = element.GetChild(index);
if( child == null )
continue;
string space = new string(' ', 2*level);
Console.WriteLine("{0}Role={1} Name={2} Value={3}",
space, child.Role.ToString(), child.Name, child.Value);
DumpElementTree(child,level);
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
int ret;
Application.SleepTime = 10;
//-----------------------------------------
// F O R M search and test
//-----------------------------------------
Form form = Application.FindFormTitle("Customer Order Viewer");
if (form == null)
{
Console.WriteLine("Cannot find application \"Customer Order Viewer\"");
return 1;
}
Console.WriteLine("Application found");
form.Activate();
//-----------------------------------------
// Find G R I D V I E W
//-----------------------------------------
Control dataGridView1 = form.FindControlName("dataGridView1");
if (dataGridView1 == null)
{
Console.WriteLine("ERROR: dataGridView1 not found ");
return 1;
}
// Dump out some elements
DumpElementTree(dataGridView1.Element, 0);
// Find the element by name
Element contactRow7 = dataGridView1.Element.FindChild(Role.Cell, "Contact Name Row 7");
if (contactRow7 != null)
contactRow7.Value = "Ranorex";
// Find cell by value
Element pedro = dataGridView1.Element.FindChildValue("Pedro Afonso",SearchMatchMode.MatchExact);
if ( pedro != null )
{
Console.WriteLine(" Name={0} Value={1}", pedro.Name, pedro.Value);
Mouse.MoveToElement(pedro);
}
Console.WriteLine("\nTEST PASSED\n");
return 0;
}
}
You cannot search a cell by value in V1.1.0, but you will be able to search an element by value in the next version.
Code: click into code to enlarge
dataGridView1.Element.FindChildValue("Pedro Afonso",SearchMatchMode.MatchExact);
You will be able to search the sub-tree of the element and return the children that match the specified value and search mode. You can also use regular expression.
Please contact us if you want to test the function Element.FindChildValue.
We can send you a trial of the next version.
Jenö
Ranorex Team |