Your inventory should be made up of slot objects that you can give IDs that match the row ID in the array. So if it is 9 slots across for example, 0-8 in the array account for the first row, then 9-17 in the array account for the second row. You don't need to move between any row of the visible inventory as the array rows (X) each refer to one slot. So the logic you have now, if 0-8 are all occupied it means the first visible row is full and it will then continue onto 9 and beyond which starts the second visible row.
To expand on the slot object variable ID in case you were not already using that, each slot can have a variable ID that refers to their place in the array. So the first slot.ID=0 and the second one is slot.ID=1. Then for example if you were to show a description on hovering mouse over the slot it would be set text to array.at(slotID,1). The slotID refers to its row in the array.