How to group a second datasource by the inventory dimensions set on the form

I have a form with a grid and inventory dimensions button.
The form displays the ItemId, ItemName, UnitId and AvailPhysical, RelatedItemIds grouped by the active dimensions (for example Site and Location).

I’ve added an 2nd grid that must show the additional data from the selected record (based on the values that are in the RelatedItems column) in the 1st grid. This grid is bound to an temp table that is filled by a class when a record in the 1st grid is selected (Active method on datasource 1st grid).

The problem is that in the 1st grid, there are dimensions set by the button (inventory dimensions) for example Site and Location. The result in the 2nd grid isn’t grouped by the selected dimensions.

How can I achieve that?