DataGrid unable to return item text

Class library usage, coding and language questions.
mvn
Posts: 9
Joined: Wed Feb 13, 2008 11:17 am

DataGrid unable to return item text

Post by mvn » Fri Oct 31, 2008 3:23 pm

I am trying to return and modify items in a data grid using the methods defined for DataGrid class. But I am having problems getting or setting the item values after the data grid control has been found.


I am Using Ranorex Pro 1.5 (.NET 2.0) DLL, the component according to the Spy is System.Windows.Forms.DataGrid. Basic (reduced) code example follows, the data grid is found and I can see the cursor "click" on
various areas in the control but the items returned are null. Its a 2 column data grid with 7 rows.

DataGrid dataGrid = form.FindDataGrid("grdParms");
dataGrid.Focus();
dataGrid.ClickColumn("Value");
string columnText = dataGrid.GetColumnText(1);
dataGrid.ClickRow(0);
dataGrid.ClickItem(1, 1);
string itemText = dataGrid.GetItemText(1, 2);
dataGrid.SetItemText(1, 2, "5");

Any suggestions on how to progress this?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Post by Support Team » Mon Nov 03, 2008 1:28 pm

I successfully tested the DataGrid class using a .NET DataGridView control. What type is your DataGrid of?

Be sure to take the right index parameters. The documentation of the DataGrid.GetItemText(int row, int column) method is a bit misleading (and will be corrected for the next release). The row and column parameters are actually 0-based, but take headers into account. I.e. if your datagrid has column headers (as usual), the first data row has index 1. However, if your datagrid does not have column headers, the first data row has index 0. The same is true for row headers and column indices.

Please check if that is the problem!

Regards,
Alex
Ranorex Support Team

mvn
Posts: 9
Joined: Wed Feb 13, 2008 11:17 am

Post by mvn » Tue Nov 04, 2008 4:28 pm

Thanks for the reply,

[quote="Support Team"]I successfully tested the DataGrid class using a .NET DataGridView control. What type is your DataGrid of?

By type what do you mean? Its a Windows.Form.DataGrid, the element is Table'DataGrid'.

[quote="Support Team"]Be sure to take the right index parameters. The documentation of the DataGrid.GetItemText(int row, int column) method is a bit misleading (and will be corrected for the next release). The row and column parameters are actually 0-based, but take headers into account. I.e. if your datagrid has column headers (as usual), the first data row has index 1. However, if your datagrid does not have column headers, the first data row has index 0. The same is true for row headers and column indices.

I have tried various parameter values but the properties always come back null. It does see the data grid using FindDataGrid("gdpParms") else it would throw an exception, debugging using VS2008 it gives one property of 2 column count but the other debug properties return function evaluation timed out message. Adding debug points to return datagrid.AccessibleRole|Name|Description also return null?

mvn
Posts: 9
Joined: Wed Feb 13, 2008 11:17 am

Post by mvn » Wed Nov 05, 2008 11:18 am

Update, added additional code to check if method returns true or false. Summary of what each method returns follows the code.

DataGrid dataGrid = form.FindDataGrid("grdParms");

int datagrdColumnCount = dataGrid.ColumnCount;

if (dataGrid.ClickColumn("Parameter Name(s)"))
{
DataManager.LibSendPassResult("Parameter Name(s)", "");
}

if(dataGrid.ClickColumn("Value"))
{
DataManager.LibSendPassResult("Value", "");
}

if (dataGrid.ClickItem(2, 1))
{
DataManager.LibSendPassResult("Click item", "2,1");
}

if (dataGrid.ClickRow(2))
{
DataManager.LibSendPassResult("Click row", "2");
}

string columnTxt0 = dataGrid.GetColumnText(0);
string columnTxt1 = dataGrid.GetColumnText(1);
string itemTxt0 = dataGrid.GetItemText(2, 1);

if (dataGrid.SetItemText(2, 1, "10"))
{
DataManager.LibSendPassResult("Set text", "2");
}

The instance of datagrid does not throw an exception, the columncount value returned is zero.

The methods ClickColumn for both columns return true, the cursor moves and clicks on each column.

The methods ClickItem, ClickRow and SetItemText return false, the data grid table contains 2 columns and 7 rows, each column has a header text label.

The GetColumnText method return "", the method GetItemText returns null.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Post by Support Team » Wed Nov 05, 2008 7:20 pm

You can also automate the grid with the Element class, check the Roles and Names with RanorexSpy.
Please try the following code with a cell:

Code: Select all

Element cell = gridControl.Element.FindChild(Role.Cell, "Name of the cell");
if (cell != null)
    Mouse.MoveToElement(cell);
Does this work?

Jenö
Ranorex Team

mvn
Posts: 9
Joined: Wed Feb 13, 2008 11:17 am

Post by mvn » Thu Nov 06, 2008 3:28 pm

Support Team wrote:You can also automate the grid with the Element class, check the Roles and Names with RanorexSpy.
Please try the following code with a cell:

Code: Select all

Element cell = gridControl.Element.FindChild(Role.Cell, "Name of the cell");
if (cell != null)
    Mouse.MoveToElement(cell);
Does this work?

Jenö
Ranorex Team
All of the Cells are labelled the same name so used this as a workaround,

Code: Select all

DataGrid globalParamsTable = form.FindDataGrid("grdParms");


                Element param = globalParamsTable.Element.FindChildValue("name of cell element", SearchMatchMode.MatchExact);
                Mouse.ClickElement(param);
                Application.SendKeys("{BKSP}", 1265);
                Application.SendKeys(<new value entered>, 500);
                Application.SendKeys("{ENTER}", 1265);
That now works so thanks for your help. I was told that the data grid I was using might not begin at row,column (0,0) but a much larger value, is there a way to read back these index values for future reference?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Post by Support Team » Mon Nov 10, 2008 11:41 am

Normally you can read the column and row info from the Name property of the cell.
If it’s not possible, then you have to write a workaround, you can use the FindChildren() method and read all the cells into a collection.

Jenö
Ranorex Team

mvn
Posts: 9
Joined: Wed Feb 13, 2008 11:17 am

Post by mvn » Wed Nov 12, 2008 8:28 am

Support Team wrote:Normally you can read the column and row info from the Name property of the cell.
If it’s not possible, then you have to write a workaround, you can use the FindChildren() method and read all the cells into a collection.

Jenö
Ranorex Team
Thanks, would this allow the actual value content of the cells to be returned without knowing what they are, I can write to the cells required with the help above but as Data Grid methods are not working need to be able to query the text fields (cells) in the grid. The following code can select the cells but the value returned (text string) is from the left hand column cell, how can I query the right hand column?

Code: Select all

 DataGrid grdParms = form.FindDataGrid("grdParms");
                grdParms.Focus();

                Element param = grdParms.Element.FindChildValue("Comment 1", SearchMatchMode.MatchExact);
                Mouse.ClickElement(param);

                param = grdParms.Element.FindChildValue("Comment 2", SearchMatchMode.MatchExact);
                Mouse.ClickElement(param);

                param = grdParms.Element.FindChildValue("Comment 3", SearchMatchMode.MatchExact);
                Mouse.ClickElement(param);

                param = grdParms.Element.FindChildValue("Comment 4", SearchMatchMode.MatchExact);
                Mouse.ClickElement(param);
The grid is a 2 column grid (with both columns having header text) then several rows below.

Thanks,

mvn
Posts: 9
Joined: Wed Feb 13, 2008 11:17 am

Post by mvn » Thu Nov 13, 2008 8:23 am

Managed to work this out but there might be a better way of doing this.

Code: Select all

 DataGrid grdParms = form.FindDataGrid("grdParms");
                grdParms.Focus();

                Element param = grdParms.Element.FindChildValue("ChildValue", SearchMatchMode.MatchExact);
                Mouse.ClickElement(param);
                param.Select(Selection.TakeFocus);
                Element[] paramArray = param.FindChildren(Role.Cell);
                string actualTxt = paramArray[1].Value;