Skip to Content
author's profile photo Former Member
Former Member

List Box in ALV Grid

Hi all,

How to insert List Box in ALV grid.

Regards,

Vijayakumar

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 07, 2007 at 12:14 PM

    Hi

    If you're using OO grid you can try to build the list box in the event ONF4, if you're using fm I don't know if it's possible.

    Max

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 07, 2007 at 09:42 PM

    Hello Vijayakumar

    Sample report

    BCALV_TEST_GRID_F4_HELP

    gives many advices on how to deal with F4 help in ALV grids. The crucial points for dropdown lists are:

    (1) We need a dropdown handle (simply a unique number)

    *§10 define data table to handle drop down boxes
    data: begin of gt_outtab occurs 0.
            include structure sflight .
    data: drop_down_handle type int4. "dropdown handle for a field
    data: style type lvc_t_styl.

    (2) Register dropdown (possible at different levels, here at the fieldcatalog)

    *§10 Register drop down at fieldcatalog.
          if drop_do = 'X' and gs_fieldcat-fieldname = 'PRICE'.
            gs_fieldcat-edit = 'X'.
            gs_fieldcat-drdn_field = 'DROP_DOWN_HANDLE'.  " => see gt_outtab definition!!!
            gs_fieldcat-drdn_alias = 'X'.
            modify gt_fieldcat from gs_fieldcat index sy-tabix.
          endif.

    (3) Define the values for the dropdown lists (identified by their handle)

    *§11 prepare grid for drop down.
        data: lt_dropdown type lvc_t_drop,
              ls_dropdown type lvc_s_drop,
              lt_dropdown_al type lvc_t_dral,
              ls_dropdown_al type lvc_s_dral.
    
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '9'.
        append ls_dropdown to lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '7'.
        append ls_dropdown to lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '89'.
        append ls_dropdown to lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '99'.
        append ls_dropdown to lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '77'.
        append ls_dropdown to lt_dropdown.
    ...
    
        ls_dropdown-handle = '3'.
        ls_dropdown-value = '17'.
        append ls_dropdown to lt_dropdown.
        ls_dropdown-handle = '3'.
        ls_dropdown-value = '33'.
        append ls_dropdown to lt_dropdown.
    ...
    
        call method my_grid->set_drop_down_table
          exporting
            it_drop_down = lt_dropdown.

    Regards

    Uwe

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 09, 2007 at 05:03 AM

    Hello Vijayakumar

    I simplified sample report BCALV_TEST_GRID_F4_HELP to show the essence of DropDown lists.

    *&---------------------------------------------------------------------
    *& Report  ZUS_SDN_BCALV_TEST_GRID_F4_1                                *
    *&                                                                     *
    *& Copied from: BCALV_TEST_GRID_F4_HELP and simplified for DropDown
    *&---------------------------------------------------------------------*
    *&                                                                     *
    *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    * Purpose:
    * ~~~~~~~~
    * This report illustrates the use of f4-Help in an alv grid control.
    *-----------------------------------------------------------------
    * Background:
    * ~~~~~~~~~~~
    * There a two possibilities to implement an f4-Help in the alv grid
    * control: one can either use standard f4-help or write one by Hand.
    * For the former there is nothing to do at all. This report shows how
    * to implement user-defined f4-help.
    *-----------------------------------------------------------------
    * The user should look at the definition of classes grid_application
    * and lcl_event_receiver:
    * all the grid-specific things happen there, while the rest
    * of the program is concerned with dynpro programming
    *---------------------------------------------------------------------
    * To check program behavior
    * ~~~~~~~~~~~~~~~~~~~~~~~~~
    * For each choice it is explained in detail which functionality the
    * f4-help dispalys. Just try out different modes at the start-dynpro.
    * We also included a message signaling when datatable update actually
    * occurs.
    *-----------------------------------------------------------------
    * Essential steps (search for '§')
    * ~~~~~~~~~~~~~~~
    * First you must define and set an event handler for event onf4
    * of class cl_gui_alv_grid, just as with any event in the object
    * model. In our case, its method on_f4 of (the local) class
    * lcl_event_receiver (definition see below).
    * We set the handler in PBO-module create_object of dynpro 100.
    *
    * For the easiest case where you don't want to make changes do step
    * 1. Register all columns for which you want to define an f4-help.
    * 1a.You can deregister columns during run-time to use standard f4-help.
    * 1b.Or register additional columns during run-time.
    * 2. Implement your event handler method.
    * 3. Set attribute m_event_handled of er_event_data to avoid standard
    *    f4-help.
    * If you want to allow the user to change data via f4-help you have to
    * 4. set in the fieldcatalog the corresponding column editable (see
    *    Documentation). It does not suffice to set the complete grid
    *    editable.
    * 5. Declare data and field-symbols for values to be changed.
    * 6. Assign the values for the corresponding cells you want to edit:
    *    you can edit any cells. Do not refresh your table!
    * 7. If your f4-help relates to other values of your table, you must
    *    set parameters getbefore and/or chngeafter during registration.
    * 8. In case the column you want to define an f4-help has no standard
    *    f4-help defined, you must set parameter F4AVAILABL in the field
    *    catalog.
    * 9. If you want to check the data yourself, you can register for the
    *    events data_changed and/or data_changed_finished. For the former
    *    you can check where the event was raised and act accordingly.
    *10. Often one uses drop down boxes instead of f4-help. To do so, you
    *    first have to extend your datatable by one field and set the
    *    fieldcatalog parameter drdn_field accordingly.
    *11. Then you have to prepare a drop down table, give it to your grid
    *    and fill in the additional field of your datatable appropritately.
    *!!  If you define a drop down box for a column, you can not define an
    *    f4-help for the same column.
    *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
    REPORT  bcalv_grid_f4_help.
    
    CLASS grid_appl DEFINITION DEFERRED.
    CLASS lcl_event_receiver DEFINITION DEFERRED.
    
    * data for report and dynpro
    DATA: info(80),
          input(20),
          ok_code LIKE sy-ucomm.
    
    * data for grid
    DATA: gs_layout TYPE lvc_s_layo,
          gt_fieldcat TYPE lvc_t_fcat,
          gs_fieldcat TYPE lvc_s_fcat.
    DATA: ret.
    *§10 define data table to handle drop down boxes
    DATA: BEGIN OF gt_outtab OCCURS 0.
            INCLUDE STRUCTURE sflight .
    DATA: drop_down_handle TYPE int4. "dropdown handle for a field
    DATA: style TYPE lvc_t_styl.
    
    
    DATA:  volume TYPE p DECIMALS 2.
    DATA:  quantity(3) TYPE c. "Color for corresponding line
    
    DATA: END OF gt_outtab,
          gs_outtab LIKE LINE OF gt_outtab.
    
    * data for event handling
    DATA: gs_f4 TYPE lvc_s_f4,
          gt_f4 TYPE lvc_t_f4.
    
    DATA: gt_outtab_test TYPE TABLE OF spfli.
    
    * custom control and grid_application object
    DATA: my_container   TYPE REF TO cl_gui_custom_container,
          my_application TYPE REF TO grid_appl.
    
    DATA: gs_variant TYPE disvariant.
    DATA: my_event_receiver TYPE REF TO lcl_event_receiver.
    
    *---------------------------------------------------------------------*
    *       CLASS grid_appl DEFINITION
    *---------------------------------------------------------------------*
    *       ........                                                      *
    *---------------------------------------------------------------------*
    CLASS grid_appl DEFINITION.
    
      PUBLIC SECTION.
        DATA: my_grid TYPE REF TO cl_gui_alv_grid.
        METHODS: constructor,
                 reset_table,
                 test_modus,
                 check_input
                   CHANGING ir_data_changed
                        TYPE REF TO cl_alv_changed_data_protocol.
    
    ENDCLASS.                    "grid_appl
    
    *---------------------------------------------------------------------*
    *       CLASS lcl_event_receiver DEFINITION
    *---------------------------------------------------------------------*
    CLASS lcl_event_receiver DEFINITION.
    
      PUBLIC SECTION.
    *    METHODS: on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
    *          IMPORTING sender
    *                 e_fieldname
    *                 e_fieldvalue
    *                 es_row_no
    *                 er_event_data
    *                 et_bad_cells
    *                 e_display.
    *                 on_data_changed FOR EVENT
    *                    data_changed OF cl_gui_alv_grid
    *       IMPORTING e_onf4
    *                 e_onf4_before
    *                 e_onf4_after
    *                 er_data_changed
    *                 e_ucomm
    *                 sender,
    *
    *    on_data_changed_finished FOR EVENT data_changed_finished
    *                         OF cl_gui_alv_grid
    *          IMPORTING sender,
    *
    *
    *    on_button_click FOR EVENT
    *          button_click OF cl_gui_alv_grid
    *          IMPORTING sender
    *          es_row_no
    *          es_col_id.
    *  PRIVATE SECTION.
    *    TYPES: ddshretval_table TYPE TABLE OF ddshretval.
    *    DATA : lr_data_changed TYPE REF TO cl_alv_changed_data_protocol.
    
    ENDCLASS.                    "lcl_event_receiver DEFINITION
    
    *----------------------------------------------------------------------*
    * SELECTION-SCREEN                                                     *
    *----------------------------------------------------------------------*
    PARAMETERS: p_maxrow TYPE i DEFAULT 60.
    SELECTION-SCREEN BEGIN OF BLOCK tab WITH FRAME TITLE text-011.
    SELECTION-SCREEN BEGIN OF LINE.
    PARAMETERS: no_inp RADIOBUTTON GROUP tab1.
    SELECTION-SCREEN COMMENT 5(15) text-017.
    PARAMETERS user_inp RADIOBUTTON GROUP tab1.
    SELECTION-SCREEN COMMENT 25(15) text-018.
    PARAMETERS drop_do RADIOBUTTON GROUP tab1 DEFAULT 'X'.
    SELECTION-SCREEN COMMENT 45(15) text-019.
    PARAMETERS test_mod RADIOBUTTON GROUP tab1.
    SELECTION-SCREEN COMMENT 65(15) text-013.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN END OF BLOCK tab.
    
    
    
    *----------------------------------------------------------------------*
    * START-OF-SELECTION                                                   *
    *----------------------------------------------------------------------*
    START-OF-SELECTION.
      gs_variant-report = sy-repid.
    
    *----------------------------------------------------------------------*
    * END-OF-SELECTION                                                     *
    *----------------------------------------------------------------------*
    END-OF-SELECTION.
      CALL SCREEN 200.
    
    *&---------------------------------------------------------------------*
    *&      Form  fill_data
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    MODULE fill_data OUTPUT.
    
    
      SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_outtab[]
    UP TO p_maxrow ROWS.                                    "#EC CI_NOWHERE
    
    
    ENDMODULE.                    " fill_data
    *---------------------------------------------------------------------*
    *       MODULE exit2 INPUT                                            *
    *---------------------------------------------------------------------*
    *       ........                                                      *
    *---------------------------------------------------------------------*
    MODULE exit2 INPUT.
    
      ret = ' '.
    
      CASE ok_code.
        WHEN 'BACK'.
          LEAVE TO SCREEN 0.
        WHEN 'CANCEL'.
          LEAVE PROGRAM.
        WHEN 'EXIT'.
          LEAVE PROGRAM.
        WHEN 'OK'.
          ret = 'X'.
      ENDCASE.
    ENDMODULE.                    "exit2 INPUT
    *&---------------------------------------------------------------------*
    *&      Module  set_status2  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE set_status2 OUTPUT.
    
      SET PF-STATUS 'MAIN200'.
      SET TITLEBAR 'MAIN200'.
    
    
    ENDMODULE.                 " set_status2  OUTPUT
    *&---------------------------------------------------------------------*
    *&      Module  create_objects  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE create_objects OUTPUT.
      IF my_container IS INITIAL.
        CREATE OBJECT my_container EXPORTING container_name = 'CONTAINER'.
        CREATE OBJECT my_application.
        CREATE OBJECT my_event_receiver.
    *    SET HANDLER my_event_receiver->on_f4 FOR ALL INSTANCES.
      ENDIF.
    ENDMODULE.                 " create_objects  OUTPUT
    
    *&---------------------------------------------------------------------*
    *&       Class (Implementation)  grid_appl
    *&---------------------------------------------------------------------*
    *        Text
    *----------------------------------------------------------------------*
    CLASS grid_appl IMPLEMENTATION.
    
      METHOD constructor.
    
    * instantiate the grid
        CREATE OBJECT my_grid
             EXPORTING i_parent = my_container.
    
        IF test_mod = 'X'.
          CALL METHOD test_modus.
        ELSE.
          CALL METHOD reset_table.
        ENDIF.
      ENDMETHOD.                    "constructor
    
    *---------------------------------------------------------------------*
    *       METHOD test_modus this method is for internal use only        *
    *---------------------------------------------------------------------*
    *       ........                                                      *
    *---------------------------------------------------------------------*
      METHOD test_modus.
    
        CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
          EXPORTING
            i_structure_name = 'SPFLI'
          CHANGING
            ct_fieldcat      = gt_fieldcat.
    
    
        CALL METHOD my_grid->set_table_for_first_display
          EXPORTING
            i_structure_name = 'SPFLI'
            is_layout        = gs_layout
            is_variant       = gs_variant
            i_save           = 'U'
          CHANGING
            it_outtab        = gt_outtab_test
            it_fieldcatalog  = gt_fieldcat.
    
        CALL METHOD my_grid->set_ready_for_input
          EXPORTING
            i_ready_for_input = '1'.
    
    
        DATA: tab TYPE lvc_t_row,
              row TYPE lvc_s_row.
    
        APPEND row TO tab.
        APPEND row TO tab.
    
        CALL METHOD my_grid->get_selected_rows
          IMPORTING
            et_index_rows = tab.
    
      ENDMETHOD.                    "test_modus
    *---------------------------------------------------------------------*
    *       METHOD reset_table                                            *
    *---------------------------------------------------------------------*
    *       ........                                                      *
    *---------------------------------------------------------------------*
      METHOD reset_table.
    
        CHECK ret EQ space.
    
    * prepare fieldcatalog
        CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
          EXPORTING
            i_structure_name = 'SFLIGHT'
          CHANGING
            ct_fieldcat      = gt_fieldcat.
    
    **§4 set those columns editable, for which user input via f4 or drop
    **   down is allowed.
        LOOP AT gt_fieldcat INTO gs_fieldcat.
    
    
    *§10 Register drop down at fieldcatalog.
          IF drop_do = 'X' AND gs_fieldcat-fieldname = 'PRICE'.
            gs_fieldcat-edit = 'X'.
            gs_fieldcat-drdn_field = 'DROP_DOWN_HANDLE'.
            gs_fieldcat-drdn_alias = 'X'.
            MODIFY gt_fieldcat FROM gs_fieldcat INDEX sy-tabix.
          ENDIF.
    *§10b Register drop down at fieldcatalog for a column wide drop down.
          IF drop_do = 'X' AND gs_fieldcat-fieldname = 'CONNID'.
            gs_fieldcat-edit = 'X'.
            gs_fieldcat-drdn_hndl = '3'.
    *        gs_fieldcat-drdn_alias = 'X'.
            MODIFY gt_fieldcat FROM gs_fieldcat INDEX sy-tabix.
          ENDIF.
    
    *§8 set parameter F4AVAILABL in the fieldcatalog if you want to define
    *   an f4-help for a column without standard f4-help.
          IF no_inp = 'X' AND gs_fieldcat-fieldname = 'PRICE'.
            gs_fieldcat-f4availabl = 'X'.
            MODIFY gt_fieldcat FROM gs_fieldcat INDEX sy-tabix.
          ENDIF.
        ENDLOOP.
    
    *§11 prepare grid for drop down.
        DATA: lt_dropdown TYPE lvc_t_drop,
              ls_dropdown TYPE lvc_s_drop,
              lt_dropdown_al TYPE lvc_t_dral,
              ls_dropdown_al TYPE lvc_s_dral.
    
    
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '9'.
        APPEND ls_dropdown TO lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '7'.
        APPEND ls_dropdown TO lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '89'.
        APPEND ls_dropdown TO lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '99'.
        APPEND ls_dropdown TO lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '77'.
        APPEND ls_dropdown TO lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '474'.
    *    append ls_dropdown to lt_dropdown.
        ls_dropdown-handle = '1'.
        ls_dropdown-value = '79'.
        APPEND ls_dropdown TO lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '57'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '964'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '94'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '78'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '389'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '399'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '377'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '3474'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '379'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '357'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '3964'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '394'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '378'.
    *    append ls_dropdown to lt_dropdown.
    *    ls_dropdown-handle = '1'.
    *    ls_dropdown-value = '454'.
    *    append ls_dropdown to lt_dropdown.
    
    
        ls_dropdown-handle = '3'.
        ls_dropdown-value = '17'.
        APPEND ls_dropdown TO lt_dropdown.
        ls_dropdown-handle = '3'.
        ls_dropdown-value = '33'.
        APPEND ls_dropdown TO lt_dropdown.
    
    
        CALL METHOD my_grid->set_drop_down_table
          EXPORTING
            it_drop_down = lt_dropdown.
    *        it_drop_down_alias = lt_dropdown_al.
    
    * sttyletest!!1
        gs_layout-stylefname = 'STYLE'.
    
        DATA: cell TYPE lvc_s_styl,
              celltab TYPE lvc_t_styl.
        cell-style = cl_gui_alv_grid=>mc_style_no_delete_row.
        APPEND cell TO celltab.
    
    
        LOOP AT gt_outtab INTO gs_outtab.
    
          IF sy-tabix = 1.
            gs_outtab-style = celltab.
          ENDIF.
    
          IF gs_outtab-connid = '0017'.
            gs_outtab-drop_down_handle = 1.
          ELSEIF gs_outtab-connid = '0026'.
            gs_outtab-drop_down_handle = 2.
          ENDIF.
          MODIFY gt_outtab FROM gs_outtab INDEX sy-tabix.
        ENDLOOP.
    
    
    
    * set table for first display
        CALL METHOD my_grid->set_table_for_first_display
          EXPORTING
    *        i_structure_name = 'SFLIGHT'
            is_layout        = gs_layout
          CHANGING
            it_outtab        = gt_outtab[]
            it_fieldcatalog  = gt_fieldcat.
    
        CALL METHOD my_grid->register_edit_event
          EXPORTING
            i_event_id = cl_gui_alv_grid=>mc_evt_enter.
    
    
        IF user_inp = 'X' OR drop_do = 'X'.
          CALL METHOD my_grid->set_ready_for_input
            EXPORTING
              i_ready_for_input = 1.
        ELSE.
          CALL METHOD my_grid->set_ready_for_input
            EXPORTING
              i_ready_for_input = 0.
        ENDIF.
    
      ENDMETHOD.                    "new_table
    
    *---------------------------------------------------------------------*
    *       METHOD check_input  our  check_data routine                   *
    *---------------------------------------------------------------------*
    *                                                                     *
    *---------------------------------------------------------------------*
      METHOD check_input.
      ENDMETHOD.                    "check_input
    ENDCLASS.               "grid_appl
    
    *---------------------------------------------------------------------*
    *       CLASS lcl_event_receiver IMPLEMENTATION
    *---------------------------------------------------------------------*
    *
    *---------------------------------------------------------------------*
    CLASS lcl_event_receiver IMPLEMENTATION.
    
    
    ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

    Regards

    Uwe

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.