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?
DataGrid unable to return item text
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
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
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
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?
[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?
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.
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.
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
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:
Does this work?
Jenö
Ranorex Team
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);
Jenö
Ranorex Team
All of the Cells are labelled the same name so used this as a workaround,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:
Does this work?Code: Select all
Element cell = gridControl.Element.FindChild(Role.Cell, "Name of the cell"); if (cell != null) Mouse.MoveToElement(cell);
Jenö
Ranorex Team
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);
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
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?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
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);
Thanks,
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;