源:百度经验 https://jingyan.baidu.com/article/e2284b2b727ffce2e7118d6c.html
运行结果:
代码:
*&---------------------------------------------------------------------*
*& Report Z_ALV_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_ALV_TEST.
TABLES: MARA.
DATA: TAB1 LIKE STANDARD TABLE OF MARA WITH HEADER LINE."定义内表
DATA: FIELDCAT TYPE LVC_T_FCAT,"定义FIELDCAT 对象
GRID TYPE REF TO CL_GUI_ALV_GRID,"定义ALV控件名
G_CONTAINER TYPE SCRFNAME VALUE 'CONT1',"重新定义本地容器对象名称
G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER."定义容器对象名
START-OF-SELECTION.
CALL SCREEN '0001'.
FORM GETDATA.
SELECT * FROM MARA INTO CORRESPONDING FIELDS OF TABLE TAB1.
ENDFORM.
FORM ALVSHOW.
CREATE OBJECT G_CUSTOM_CONTAINER "创建容器对象
EXPORTING CONTAINER_NAME = G_CONTAINER.
CREATE OBJECT GRID "创建ALV对象
EXPORTING I_PARENT = G_CUSTOM_CONTAINER.
CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY "调用ALV显示方法
EXPORTING I_STRUCTURE_NAME = 'MARA' "参考的数据库表,上面我们已经定义了参考MARA
CHANGING IT_OUTTAB = TAB1[]
IT_FIELDCATALOG = FIELDCAT[].
ENDFORM.
"菜单事件
MODULE USER_COMMAND_0001 INPUT. "屏幕中的PAI事件模块
CASE SY-UCOMM.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'EXEC'.
PERFORM GETDATA.
PERFORM ALVSHOW.
ENDCASE.
ENDMODULE.
MODULE STATUS_0001 OUTPUT. "屏幕中的PBO事件模块
SET PF-STATUS 'T001'."菜单名
ENDMODULE.