How to Select a Cell with a Value

Class library usage, coding and language questions.
kimballae
Posts: 1
Joined: Tue May 08, 2007 10:02 pm

How to Select a Cell with a Value

Post by kimballae » Tue May 08, 2007 10:07 pm

How do you select a child element that is a Cell that has a specific value? When I use Ranorex Spy the current value is displayed. I assume there is some way to select the specific cell.

BTW - This is a really cool product, good job.

andypicch
Posts: 1
Joined: Wed May 09, 2007 4:05 pm

Working with grids

Post by andypicch » Wed May 09, 2007 4:10 pm

Yeah, I think it would be great to see some sample code on how to handle grids in general. How to retrieve and post data to specific cells and how to search cells, rows and columns for specific data... if any or all of that is possible.

admin
Site Admin
Site Admin
Posts: 350
Joined: Wed Jul 05, 2006 7:44 pm

Post by admin » Wed May 09, 2007 8:39 pm

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: Select all

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: Select all

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