How to: Access data from a WPF DataGrid

Best practices, code snippets for common functionality, examples, and guidelines.
adamlangstaff
Posts: 3
Joined: Mon Dec 06, 2021 8:40 am

How to: Access data from a WPF DataGrid

Post by adamlangstaff » Mon Dec 06, 2021 8:46 am

I’m using Ranorex to write some automated tests for my WPF application. I have a WPF DataGrid and I’m having difficulty accessing the cells within a row from the user code. I can return a list of rows but just not the data within.

Spy seems to successfully track and identify the control as it displays the row and each cell within. There is however one concern. All the rows seem to be within a container. I’m using the latest WPF plugin (WPFimprovedonly).

I’ve seen suggestions that if the table is within containers then I need maybe need to create an adapter. I don’t know if I’m doing this correctly.

Things I’ve tried:

MyTestRepo repo = MyTestRepo.Instance;

Attempt 1:

IList rowList = repo.Folder.DataGrid.FindDescendants<Row>();

var rowCount = rowList.Count;

This returns a list of rows but then I can’t access any further data because all cells within each row seem to timeout.

Attempt 2:

var tableAdapter = repoItem.Folder.DataGrid.As <Ranorex.Table>();

var rowCount = tableAdapter.Rows.Count;

var cellCount = tableAdapter.Rows[0].Cells.Count;

This returns a count of rows but then I can’t access any cells within as the cellCount is always Zero.

Any suggestions on the best way to access data from a DataGrid without the cells timing out?

Thanks very much.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7465
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to: Access data from a WPF DataGrid

Post by odklizec » Mon Dec 06, 2021 7:55 pm

Hi,

Please upload a Ranorex snapshot (NOT screenshot) of the grid in question. Without seeing the snapshot, it's impossible to suggest a reliable solution.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

adamlangstaff
Posts: 3
Joined: Mon Dec 06, 2021 8:40 am

Re: How to: Access data from a WPF DataGrid

Post by adamlangstaff » Tue Dec 07, 2021 9:21 am

Hi - I have attached a snapshot of the table.

I have managed to find a bit of a workaround but this relies on me knowing a row exists.
If the table has a row - I can search (track) this rows name cell directly and perform the following code:
var cells = expendableRow.Parent.Children;
This returns a list of cells for that row.

It would be better if I could access rows and cells from the top level table, instead of relying on a row already existing.
Just to note - its now returning row count as zero when searching on the top level table.

Thanks.
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7465
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to: Access data from a WPF DataGrid

Post by odklizec » Tue Dec 07, 2021 10:55 am

Hi,

Well, the problem is, that this table implementation is somewhat non-standard. The reason why var cellCount = tableAdapter.Rows[0].Cells.Count returns zero is, because the cells are not directly under row, but they are stored in additional List.
What you can do is to get the list of rows, using this xpath:
/form[@title='DAC MDPP']//table[@automationid='Expendables']//Row
And then using for..each loop enumerate each row and find all cells for each row. Here is a pseudocode you can try to adapt...

Code: Select all


IList<Ranorex.Row> rowElements = rowElementInfo.CreateAdapters<Ranorex.Row>();  //get list of rows
foreach (Ranorex.Row rowElement in rowElements)
{
    IList<Ranorex.Cell> cellElements = rowElement.Find<Ranorex.Cell>(".//cell") ;  //get list of cells for given row
    foreach (Ranorex.Cell cellElement in cellElements)
    {
        //do whatever you want with each cell for given row
    }
}
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

adamlangstaff
Posts: 3
Joined: Mon Dec 06, 2021 8:40 am

Re: How to: Access data from a WPF DataGrid

Post by adamlangstaff » Tue Dec 07, 2021 4:15 pm

Thanks for your prompt response.
I have attached another snapshot of another DataGrid which displays rows in a container.
How would it be possible to access data from this.
It must be noted that both snapshots are produced from standard WPF DataGrids that have been implemented in the same manner.
It seems very strange that Ranorex is identifying these controls differently.
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7465
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to: Access data from a WPF DataGrid

Post by odklizec » Tue Dec 07, 2021 4:27 pm

Hi,

Well, I'm afraid, I don't have an explanation for this behavior. However, you are using an outdated and no longer supported Ranorex version. Additionally, there is a technology limitation warning shown by both snapshots and caused by different DPI settings of your monitors. Try most recent Ranorex version and set the same display scale of all connected monitors.
DifferentDPI.png
You do not have the required permissions to view the files attached to this post.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration