ALV – Keep selection and scroll position after ALV refresh

SAPThere is an issue with ALV grid that if you refresh the grid being in edit mode using method REFRESH_TABLE_DISPLAY (in case you use CL_GUI_ALV_GRID) after you have changed some data, it may happen the cursor and/or scroll goes to the first (top left) cell of the grid. It might be a little confusing for user, but you can avoid this issue by using the following methods (and attached piece of code)

  1. GET_SCROLL_INFO_VIA_ID – gets current scroll information
  2. GET_CURRENT_CELL – gets current cell, we will use that data to set the cursor at the end
  3. GET_SELECTED_ROWS – gets selected rows
  4. GET_SELECTED_CELLS_ID – if we didn’t select any rows, we check for the selected cell information
  5. REFRESH_TABLE_DISPLAY – refresh the grid with updated data
  6. SET_SELECTED_CELLS_ID – set selected cell
  7. SET_SELECTED_ROWS – or set seleted rows (depanding what we received at the begining)
  8. SET_SCROLL_INFO_VIA_ID – refresh the previous scroll position
  9. SET_CURRENT_CELL_VIA_ID – Set cursor on the last selected cell

In the below code (class method), there is just an example code of the methods mentioned in the list above.

Method definition – Importing parameters

i_soft         TYPE xfeld  DEFAULT abap_true 
i_set_current  TYPE xfeld  DEFAULT abap_true
i_set_selected TYPE xfeld  DEFAULT abap_false

… changing parameters

ir_grid TYPE REF TO cl_gui_alv_grid

… and the code implementation itself

METHOD refresh_grid.
    l_scroll_row_no   TYPE lvc_s_roid,
    l_scroll_row_info TYPE lvc_s_row,
    l_scroll_col_info TYPE lvc_s_col,
    l_cell_row_no TYPE lvc_s_roid,
    l_cell_row_id TYPE lvc_s_row,
    l_cell_col_id TYPE lvc_s_col,

    mt_sel_cells TYPE lvc_t_ceno,
    mt_sel_rows  TYPE lvc_t_row.

* Save the scroll info
    es_row_no   = l_scroll_row_no
    es_row_info = l_scroll_row_info
    es_col_info = l_scroll_col_info

* Save info about last selected cell
*     e_row     = 
*     e_value   = 
*     e_col     = 
      es_row_id = l_cell_row_id
      es_col_id = l_cell_col_id
      es_row_no = l_cell_row_no

* Save info about selected rows
      et_index_rows = mt_sel_rows

* If no row is selected save info about selected cells
  IF mt_sel_rows[] IS INITIAL.
        et_cells = mt_sel_cells 

* ALV Grid refresh
  mr_grid->refresh_table_display( i_soft_refresh = i_soft ).

* Restore the saved selection
  IF i_set_selected = abap_true.
    IF mt_sel_cells[] IS NOT INITIAL.
      mr_grid->set_selected_cells_id( it_cells = mt_sel_cells   ).
        it_index_rows            = mt_sel_rows
*       it_row_no                = 
*       is_keep_other_selections = 

* Restore previously saved scroll position
    is_row_info = l_scroll_row_info
    is_col_info = l_sroll_col_info
    is_row_no   = l_scroll_row_no

* Set focus on previously selected cell
  IF i_set_current = abap_true.
      is_row_id    = l_cell_row_id
      is_column_id = l_cell_col_id
      is_row_no    = l_cell_row_no ).

3 thoughts on “ALV – Keep selection and scroll position after ALV refresh

Leave a Reply