Display a Secondary List using ALV Grid

To display a secondary list when you click on one of the row items in an alv grid. The secondary list should also be an alv.

Try out this code. You will have to make a structure ZSTR same as the output internal table.
 

REPORT ZTEST_REP1 .

TABLES : MARA,
         BHDGD,
         zstr.

TYPES:     BEGIN OF T_MARA,
              MATNR LIKE MARA-MATNR,
              ERNAM LIKE MARA-ERNAM,
           END OF T_MARA.
 

CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
*Constants for ALV Implementation
CONSTANTS:  C_SET                        VALUE 'X',
            C_RESET                      VALUE '0',
            C_SAVE                       VALUE 'A',
            C_EXIT(4)                    VALUE 'EXIT',
            C_BACK(4)                    VALUE 'BACK',
            C_CANC(4)                    VALUE 'CANC',
            C_PGTOP(5)                   VALUE 'PGTOP',
            C_PGUP(4)                    VALUE 'PGUP',
            C_PGDN(4)                    VALUE 'PGDN',
            C_PGEND(5)                   VALUE 'PGEND'.
 
 

DATA : I_MARA TYPE STANDARD TABLE OF T_MARA WITH HEADER LINE,

*      Internal table for fields catalouge
       I_FIELDCAT TYPE LVC_T_FCAT WITH HEADER LINE,
*       i_fieldcat2 type lvc_t_fcat with header line,

*      Internal table for cursor position
       I_GT_SELROWS TYPE LVC_T_ROW .

DATA : WA_MARA LIKE I_MARA,
       WA_GRIDROW LIKE LVC_S_ROW,
       WA_GRIDCOL LIKE LVC_S_COL.

*Data for ALV Implementation.
DATA:  OK_CODE       LIKE SY-UCOMM,
       W_OK_CODE     LIKE SY-UCOMM,
       W_CALL        TYPE I VALUE 1,
       W_TAB         LIKE SY-UCOMM VALUE 'TAB1',
       W_SAVE,                                 "For Parameter I_SAVE
       W_VARIANT     TYPE DISVARIANT,          "For parameter IS_VARIANT
       W_GRID        TYPE REF TO CL_GUI_ALV_GRID,
*       w_grid1       type ref to cl_gui_alv_grid,
       W_CONTAINER  TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

*       w_container1  type ref to cl_gui_custom_container,

       W_REPID       LIKE SY-REPID,
       W_GS_PRINT    TYPE LVC_S_PRNT,
       W_GS_LAYOUT   TYPE LVC_S_LAYO,
       W_EVENT_REC   TYPE REF TO LCL_EVENT_RECEIVER,
       W_CONT_MAIN   TYPE SCRFNAME VALUE 'CCCONTAINER',
       W_LN          TYPE I,                            "line number
       W_INDEX       LIKE SY-TABIX,
       W_FLAG,
       W_TEMP_VAL    TYPE I.

*---------------------------------------------------------------------*
* Definition:
*---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION.

  PUBLIC SECTION.
    METHODS:
    HANDLE_TOP_OF_PAGE
        FOR EVENT PRINT_TOP_OF_PAGE OF CL_GUI_ALV_GRID,
    HANDLE_DOUBLE_CLICK
        FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
                IMPORTING E_ROW E_COLUMN.
ENDCLASS.
 

*---------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
  METHOD HANDLE_TOP_OF_PAGE.
    PERFORM F_GET_HEADER.
  ENDMETHOD.                           "handle_top_of_page

  METHOD HANDLE_DOUBLE_CLICK.

*   The event DOUBLE_CLICK provides parameters for row and column
*   of the click. We use row parameter to select a line of the
*   corresponding internal table.

*   read selected row from internal table
      READ TABLE I_MARA INDEX E_ROW-INDEX INTO WA_MARA.
      IF SY-SUBRC <> 0.
*        message i001. " Cursor position not correct.
      ELSE.
*       call dialog screen and display the details
        call screen 200 starting at 10 5.
      ENDIF.
 
 

  ENDMETHOD.                           "handle_double_click

ENDCLASS.

*-----------------------------------------------------------------------
* start-of-selection.
*-----------------------------------------------------------------------
START-OF-SELECTION.

SELECT MATNR ERNAM FROM MARA INTO TABLE I_MARA.

*----------------------------------------------------------------------
* End-of-Selection.
*----------------------------------------------------------------------
END-OF-SELECTION.

* Start of ALV part.
  W_REPID = SY-REPID.
  W_VARIANT-REPORT = W_REPID.
  W_SAVE = C_SAVE.
  W_CONT_MAIN = W_CONT_MAIN.
  W_GS_LAYOUT = W_GS_LAYOUT.
  W_GS_PRINT  = W_GS_PRINT.
  I_FIELDCAT  = I_FIELDCAT.
  CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Form  f_get_header
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM F_GET_HEADER.

  DATA: L_LINE1 LIKE BHDGD-LINE1,
        L_LINE2 LIKE BHDGD-LINE2.
  CONSTANTS LC_SPACE VALUE ' '.
  DATA: L_F1(7), L_F2(11), L_F3(9), L_F4(6), L_F5(11), L_F6(4), L_F7(8),
        L_F8(4),L_F9(10), L_F11(11), L_F12(24), L_F13(4),
        L_F14(3).

* take the values of line1 and line2 into two new variables, otherwise
* after coming back to the first screen from the print preview, the
* header shows the condensed lines

  L_LINE1 = BHDGD-LINE1.
  L_LINE2 = BHDGD-LINE2.

  CONDENSE L_LINE1.
  CONDENSE L_LINE2.

*split the lines to display the whole lines within the
*stipulated report-width
 SPLIT L_LINE1 AT LC_SPACE INTO L_F1 L_F2 L_F3 L_F4 L_F5 L_F6 L_F7 L_F8
                                    L_F9 .

  SPLIT L_LINE2 AT LC_SPACE INTO L_F11 L_F12 L_F13 L_F14.
  L_F14 = SY-PAGNO.
    WRITE:/1 L_F1, 9 L_F2, 40 L_F3, 50 L_F4, 57 L_F5, 88 L_F6, 93 L_F7 ,
           103 L_F8 , 108 L_F9 .
    WRITE:/1 L_F11, 40 TEXT-012, 78 L_F12, 103 L_F13, 108 L_F14.
 

ENDFORM.                    " f_get_header
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STAT'.
  SET TITLEBAR 'TITL'.

ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CASE SY-UCOMM .
    WHEN C_EXIT OR C_BACK OR C_CANC.
      IF NOT W_CONTAINER  IS INITIAL.
        CALL METHOD W_CONTAINER->FREE.
      ENDIF.

      LEAVE TO SCREEN 0.

    WHEN C_PGTOP.
      WA_GRIDROW-INDEX = 1.

    WHEN C_PGUP.
      IF WA_GRIDROW-INDEX <= 15.
        WA_GRIDROW-INDEX = 1.
      ELSE.
        WA_GRIDROW-INDEX = WA_GRIDROW-INDEX - 15.
      ENDIF.

    WHEN C_PGDN.
      PERFORM F_GET_NO_ROWS.
      W_TEMP_VAL = W_LN - WA_GRIDROW-INDEX.
      IF W_TEMP_VAL < 15.
        WA_GRIDROW-INDEX = W_LN.
      ELSE.
        WA_GRIDROW-INDEX = WA_GRIDROW-INDEX + 15.
      ENDIF.

    WHEN C_PGEND.
      PERFORM F_GET_NO_ROWS.
      WA_GRIDROW-INDEX = W_LN.

  ENDCASE.
 

ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  f_get_no_rows
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM F_GET_NO_ROWS.

    DESCRIBE TABLE I_MARA  LINES W_LN.

ENDFORM.                    " f_get_no_rows
*&---------------------------------------------------------------------*
*&      Module  DISPLAY_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE DISPLAY_0100 OUTPUT.

  IF NOT WA_GRIDROW IS INITIAL
  AND NOT WA_GRIDCOL IS INITIAL.

    CALL METHOD W_GRID->SET_SCROLL_INFO_VIA_ID
      EXPORTING
        IS_ROW_INFO = WA_GRIDROW
        IS_COL_INFO = WA_GRIDCOL .

    CALL METHOD W_GRID->SET_CURRENT_CELL_VIA_ID
      EXPORTING
        IS_ROW_ID = WA_GRIDROW
        IS_COLUMN_ID = WA_GRIDCOL .

  ENDIF.

  CALL METHOD W_GRID->GET_SCROLL_INFO_VIA_ID
  IMPORTING
    ES_ROW_INFO = WA_GRIDROW
    ES_COL_INFO = WA_GRIDCOL .

  CALL METHOD W_GRID->GET_SELECTED_ROWS
    IMPORTING
      ET_INDEX_ROWS = I_GT_SELROWS[].

* Build the fieldcat according to structure
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
       EXPORTING
            I_STRUCTURE_NAME = 'ZSTR'
       CHANGING
            CT_FIELDCAT      = I_FIELDCAT[].

  LOOP AT I_FIELDCAT.
    W_INDEX = SY-TABIX.
    CASE I_FIELDCAT-FIELDNAME.

      WHEN 'MATNR'.
        I_FIELDCAT-SCRTEXT_S = 'MATNR'.
        I_FIELDCAT-KEY       = ' '.
        I_FIELDCAT-COL_POS   = '1'.

      WHEN 'ERNAM'.
        I_FIELDCAT-SCRTEXT_S = 'ERDAT'.
        I_FIELDCAT-OUTPUTLEN = '18'.
        I_FIELDCAT-COL_POS   = '2'.
 
 

    ENDCASE.
    MODIFY I_FIELDCAT INDEX W_INDEX.

  ENDLOOP.

  READ TABLE I_FIELDCAT INDEX 1 .

  IF W_CALL = 1.

    PERFORM F_STD_HEADER.

    CALL METHOD W_GRID->SET_TABLE_FOR_FIRST_DISPLAY
                      EXPORTING
                        IS_VARIANT                    = W_VARIANT
                        I_SAVE                        = W_SAVE
                      CHANGING
                        IT_OUTTAB                     = I_MARA[]
                        IT_FIELDCATALOG               = I_FIELDCAT[]
                     EXCEPTIONS
                       INVALID_PARAMETER_COMBINATION = 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.
      EXIT.
    ENDIF.

    CREATE OBJECT W_EVENT_REC.
    SET HANDLER W_EVENT_REC->HANDLE_TOP_OF_PAGE FOR W_GRID.
    CREATE OBJECT W_EVENT_REC.
    SET HANDLER W_EVENT_REC->HANDLE_DOUBLE_CLICK FOR W_GRID.
    W_FLAG = C_RESET.
 

    CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = W_GRID.
    W_CALL = 0.
  ENDIF.
 

ENDMODULE.                 " DISPLAY_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  f_std_header
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM F_STD_HEADER.

ENDFORM.                    " f_std_header
*&---------------------------------------------------------------------*
*&      Module  DYNPRONR_CHECK_500  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE DYNPRONR_CHECK_500 OUTPUT.
*  if w_dynpronr is initial.
*    w_dynpronr = '0100'.
*  endif.

  ENDMODULE.                 " DYNPRONR_CHECK_500  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  create_objects_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE create_objects_0100 OUTPUT.
  check w_container is initial .
  create object w_container
    exporting
       container_name = 'CC'.
  create object w_grid
    exporting
       i_parent = w_container.
  w_flag = c_set.
  w_flag = w_flag.
ENDMODULE.                 " create_objects_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  STATUS_0200  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0200 OUTPUT.
  SET PF-STATUS 'ST20'.
  SET TITLEBAR '200'.
  zstr-matnr    = wa_mara-matnr.
  zstr-ernam    = wa_mara-ernam.

ENDMODULE.                 " STATUS_0200  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0200  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0200 INPUT.
  move ok_code to w_ok_code.
  clear ok_code.
  case w_ok_code.
    when c_back or c_exit or c_canc.
      leave to screen 0.
  endcase.
  clear w_ok_code.
ENDMODULE.                 " USER_COMMAND_0200  INPUT

*-- End of Program

Questions
Do you have a ABAP Question?

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.