Grid subrow expand/contract all

Is there a way to have a button/link onClick event to expand all subrows visible on the screen (assuming paging is enabled)?

I envision a scenario where the user will want to expand the subrows before print view or to simply view all subrows.

Keep in mind that not all rows will have subrows.

The next code can be used to open all sub-rows(grids) on current page

var state = mygrid.getStateOfView(); //get details about current page

for (var i = state[1]; i<state[2] ; i++){ //for all rows on the page
var cell = mygrid.cellByIndex(i,CINDEX);
if (; //open sub