Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How to use ALV Grid?

Hi,

I am totally new to ALV Grids. Could you give a full example of creating a ALV Grid with 2 columns, thinking there is a internal table itab with itab-col1 & itab-col2. Please start from initialising variables and onwards.

Thanks,

Kishan

Tags:
Former Member
Former Member replied

A small sample code

*&---------------------------------------------------------------------*
*& Report  Z_TEST_GRID                                                 *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  Z_TEST_GRID                             .


*Data Declaration
*----------------
DATA: BEGIN OF T_EKKO,
  EBELN TYPE EKPO-EBELN,
  EBELP TYPE EKPO-EBELP,
  FLAG TYPE C,
 END OF T_EKKO.

  DATA: GD_REPID LIKE SY-REPID, "Exists
  REF_GRID TYPE REF TO CL_GUI_ALV_GRID. "new

DATA: BEGIN OF IT_EKKO OCCURS 0.
        INCLUDE STRUCTURE T_EKKO.
DATA: END OF IT_EKKO.

DATA: BEGIN OF IT_BACKUP OCCURS 0.
        INCLUDE STRUCTURE T_EKKO.
DATA: END OF IT_BACKUP.
*ALV data declarations
TYPE-POOLS: SLIS.                                 "ALV Declarations
DATA: FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
      GD_LAYOUT    TYPE SLIS_LAYOUT_ALV.


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

  PERFORM DATA_RETRIEVAL.
  PERFORM BUILD_FIELDCATALOG.
  PERFORM BUILD_LAYOUT.
  IT_BACKUP[] = IT_EKKO[].
  PERFORM DISPLAY_ALV_REPORT.


*&--------------------------------------------------------------------*
*&      Form  build_fieldcatalog
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM BUILD_FIELDCATALOG.
  REFRESH FIELDCATALOG.
  CLEAR FIELDCATALOG.
*
   FIELDCATALOG-FIELDNAME   = 'FLAG'.
  FIELDCATALOG-SELTEXT_M   = 'Check'.
  FIELDCATALOG-INPUT     = 'X'.
  FIELDCATALOG-EDIT     = 'X'.
  fieldcatalog-checkbox = 'X'.
  FIELDCATALOG-COL_POS     = 1.
  APPEND FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'EBELN'.
  FIELDCATALOG-SELTEXT_M   = 'Purchase Order'.
  FIELDCATALOG-INPUT     = 'X'.
  FIELDCATALOG-EDIT     = 'X'.
  FIELDCATALOG-COL_POS     = 2.
  APPEND FIELDCATALOG.
  CLEAR  FIELDCATALOG.

  FIELDCATALOG-FIELDNAME   = 'EBELP'.
  FIELDCATALOG-SELTEXT_M   = 'PO Item'.
  FIELDCATALOG-COL_POS     = 3.
  APPEND FIELDCATALOG.
  CLEAR  FIELDCATALOG.

ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM BUILD_LAYOUT.
  "Permet d'ajuster les colonnes au text
*  gd_layout-colwidth_optimize = 'X'.
*  GD_LAYOUT-TOTALS_TEXT       = 'Totals'(201).

*  gd_layout-box_fieldname = 'SELECT'.
*  gd_layout-box_tabname   = 'IT_EKKO'.

ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM DISPLAY_ALV_REPORT .
  GD_REPID = SY-REPID.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            I_CALLBACK_PROGRAM        = GD_REPID
*            i_callback_top_of_page   = 'TOP-OF-PAGE'
*            I_CALLBACK_PF_STATUS_SET  = 'SET_PF_STATUS'
*            I_CALLBACK_USER_COMMAND   = 'USER_COMMAND'
*            i_grid_title             = 'My Title'
            IS_LAYOUT                 = GD_LAYOUT
            IT_FIELDCAT               = FIELDCATALOG[]
       TABLES
            T_OUTTAB                  = IT_EKKO
       EXCEPTIONS
            PROGRAM_ERROR             = 1
            OTHERS                    = 2.

  IF SY-SUBRC <> 0.
    WRITE:/ SY-SUBRC.
  ENDIF.

ENDFORM.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM DATA_RETRIEVAL.
  SELECT EBELN EBELP
   UP TO 10 ROWS
    FROM EKPO
    INTO CORRESPONDING FIELDS OF TABLE  IT_EKKO.
ENDFORM.                    " DATA_RETRIEVAL


*----------------------------------------------------------------------*
*                      FORM SET_PF_STATUS                              *
*----------------------------------------------------------------------*
FORM SET_PF_STATUS USING RT_EXTAB   TYPE  SLIS_T_EXTAB.
  SET PF-STATUS 'STANDARD_FULLSCREEN1' EXCLUDING RT_EXTAB.
ENDFORM.                    "set_pf_status

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question