This program demonstrates how to get the updated data when a button is clicked in the GUI toolbar or ALV toolbar. The ALV is created using CL_GUI_ALV_GRID.


REPORT  zzsourav_refresh_alv.DATA: ok_code TYPE syucomm.*----------------------------------------------------------------------*
*       CLASS main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS main DEFINITION.
  PUBLIC SECTION.
    DATA: i_sbook TYPE STANDARD TABLE OF sbook INITIAL SIZE 0,
          i_sbook_old TYPE STANDARD TABLE OF sbook INITIAL SIZE 0.
 
    METHODS:constructor,
            handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING e_object e_interactive,
            handle_user_command FOR EVENT
            user_command OF cl_gui_alv_grid
            IMPORTING e_ucomm,
            check_changed_data.
  PROTECTED SECTION.
 
 
  PRIVATE SECTION.
    TYPE-POOLS: icon.
    DATA:container TYPE REF TO cl_gui_custom_container,
          oref_alv TYPE REF TO cl_gui_alv_grid,
          wa_layout TYPE lvc_s_layo,
          i_fieldcatalog TYPE lvc_t_fcat.
    METHODS:get_field_catalog,get_layout.ENDCLASS.                    "main DEFINITION
*----------------------------------------------------------------------*
*       CLASS main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS main IMPLEMENTATION.
  METHOD constructor.
 
    SELECT * FROM sbook INTO TABLE i_sbook
      UP TO 50 ROWS.
    i_sbook_old[] = i_sbook[].
 
    CREATE OBJECT container
      EXPORTING
*       parent                      =
        container_name              = 'CONT1'
*       style                       =
*       lifetime                    = lifetime_default
        repid                       = sy-repid
        dynnr                       = '0100'
*       no_autodef_progid_dynnr     =
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5
        OTHERS                      = 6
        .
    IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
 
 
    CREATE OBJECT oref_alv
      EXPORTING
*        i_shellstyle      = 0
*        i_lifetime        =
         i_parent          = container
*        i_appl_events     = space
*        i_parentdbg       =
*        i_applogparent    =
*        i_graphicsparent  =
*        i_name            =
*        i_fcat_complete   = space
      EXCEPTIONS
        error_cntl_create = 1
        error_cntl_init   = 2
        error_cntl_link   = 3
        error_dp_create   = 4
        OTHERS            = 5
        .
    IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
 
    CALL METHOD me->get_field_catalog.
    CALL METHOD me->get_layout.
 
    CALL METHOD oref_alv->set_table_for_first_display
      EXPORTING
*       i_buffer_active               =
*       i_bypassing_buffer            =
*       i_consistency_check           =
*       i_structure_name              =
*       is_variant                    =
*       i_save                        =
*       i_default                     = 'X'
        is_layout                     = wa_layout
*       is_print                      =
*       it_special_groups             =
*       it_toolbar_excluding          =
*       it_hyperlink                  =
*       it_alv_graphics               =
*       it_except_qinfo               =
*       ir_salv_adapter               =
      CHANGING
        it_outtab                     = i_sbook
        it_fieldcatalog               = i_fieldcatalog
*       it_sort                       =
*       it_filter                     =
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4
            .
    IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    SET HANDLER me->handle_toolbar FOR oref_alv.
    SET HANDLER me->handle_user_command FOR oref_alv.
 
    CALL METHOD oref_alv->set_toolbar_interactive.
 
  ENDMETHOD.                    "constructor
  METHOD get_layout.
    wa_layout-cwidth_opt = 'X'.
  ENDMETHOD.                    "get_layout
  METHOD get_field_catalog.
    FIELD-SYMBOLS: <lf1> TYPE lvc_s_fcat.
 
    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
*       I_BUFFER_ACTIVE              =
        i_structure_name             = 'SBOOK'
        i_client_never_display       = 'X'
*       I_BYPASSING_BUFFER           =
        i_internal_tabname           = 'I_SBOOK'
      CHANGING
        ct_fieldcat                  = i_fieldcatalog
      EXCEPTIONS
        inconsistent_interface       = 1
        program_error                = 2
        OTHERS                       = 3
              .
    IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
 
    LOOP AT i_fieldcatalog ASSIGNING <lf1>.
      IF <lf1>-fieldname = 'SMOKER'.
        <lf1>-edit = 'X'.  " REUSE_ALV_GRID_DISPLAY
      ENDIF.
    ENDLOOP.
  ENDMETHOD.                    "get_field_catalog
  METHOD handle_toolbar.
    DATA: l_toolbar  TYPE stb_button.
    CLEAR l_toolbar.* append an icon to show booking table
    CLEAR l_toolbar.
    MOVE 'SAVE' TO l_toolbar-function.
    MOVE icon_system_save TO l_toolbar-icon.
    MOVE 'Save' TO l_toolbar-quickinfo.
    MOVE 'Save' TO l_toolbar-text.
    MOVE ' ' TO l_toolbar-disabled.
    APPEND l_toolbar TO e_object->mt_toolbar.
 
  ENDMETHOD.                    "handle_toolbar
  METHOD handle_user_command.
    CASE e_ucomm.
      WHEN 'SAVE'.
        IF i_sbook[] <> i_sbook_old[].
          MESSAGE i001(00) WITH 'You have changed some data'.
        ELSE.
          MESSAGE i001(00) WITH 'No data changed'.
        ENDIF.
    ENDCASE.
  ENDMETHOD.                    "user_command
  METHOD check_changed_data.
 
    CALL METHOD oref_alv->check_changed_data
*      IMPORTING
*        e_valid   =
*      CHANGING
*        c_refresh = 'X'
        .
 
 
  ENDMETHOD.                    "CHECK_CHANGED_DATA
ENDCLASS.                    "main IMPLEMENTATION
 START-OF-SELECTION.
 
  DATA: oref_main TYPE REF TO main.
  CREATE OBJECT oref_main.
 
  CALL SCREEN 0100.*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'S0100'.*  SET TITLEBAR 'xxx'.
 ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
 
  CASE ok_code.
    WHEN 'BACK'.
 
      SET SCREEN 00.
      LEAVE SCREEN.
    WHEN 'SAVE'.
      " Check this part if the button is SAP standard toolbar
      CALL METHOD oref_main->check_changed_data.
      IF oref_main->i_sbook[] NE oref_main->i_sbook_old[].
        MESSAGE i001(00) WITH 'You have changed some data'.
      ELSE.
        MESSAGE i001(00) WITH 'No data changed'.
      ENDIF.
 
  ENDCASE.ENDMODULE.                 " USER_COMMAND_0100  INPUT
 
 
 
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com