smoothlikebutter
smoothlikebutter

Reputation: 115

Sorting in an cl_gui_alv_grid by column?

I'm generating an internal table with cl_gui_alv_grid that has five columns and I can't seem to figure out how to sort by column 1 once the table is loaded. I managed to do it with cl_salv_table, however not with the grid. Any idea please? I suppose it's done somehow with the CALL METHOD go_alv->set_sort_criteria however I'm getting a crash when triggering the table view.

DATA: lt_sort TYPE lvc_t_sort,
      ls_sort TYPE lvc_s_sort.
FORM sort_data.
   REFRESH: lt_sort.
   CLEAR: ls_sort.
   ls_sort-spos = '1'.
   ls_sort-fieldname = 'Column1'.
   ls_sort-up        = abap_true.
   APPEND ls_sort TO lt_sort.
ENDFORM.
FORM first_display.
   PERFORM sort_data.
   CALL METHOD go_alv->set_table_for_first_display
   EXPORTING
      i_structure_name = 'TABLE_STRUCTURE'
      is_layout        = gs_layout
   CHANGING
      it_outtab        = gt_salv_table
ENDFORM.
CALL METHOD go_alv->set_sort_criteria
    EXPORTING
      it_sort = lt_sort
*    EXCEPTIONS
*     no_fieldcatalog_available = 1
*     others  = 2.

Upvotes: 1

Views: 2895

Answers (1)

József Szikszai
József Szikszai

Reputation: 5071

Do you call the set_sort_criteria before the set_table_for_first_display? It looks like there is no fieldcatalouge yet, this is why it does not work.

I think more straighforward is: The set_table_for_first_display method has a changing parameter it_sort, just use that one for importing the sort table into the class, no need to call set_sort_criteria:

   CALL METHOD go_alv->set_table_for_first_display
   EXPORTING
      i_structure_name = 'TABLE_STRUCTURE'
      is_layout        = gs_layout
   CHANGING
      it_outtab        = gt_salv_table
      it_sort          = lt_sort
   EXCEPTIONS
      ...

Upvotes: 2

Related Questions