进入ABAP程序编辑器界面,敲打以下程序。
REPORT ZALV999.
TABLES:MARA."这里用了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.
*-----------------------------------*
* 产生ALV子程序
*-----------------------------------*
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.