效果如下图:
程序:
REPORT z_barry_alv_zero.
DATA: itab TYPE STANDARD TABLE OF zstru001 .
DATA: ok_code LIKE sy-ucomm ,
save_ok LIKE sy-ucomm .
DATA: go_grid TYPE REF TO cl_gui_alv_grid,
go_custom_container TYPE REF TO cl_gui_custom_container.
DATA: gt_fieldcat TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat,
sla TYPE lvc_s_layo .
START-OF-SELECTION.
CALL SCREEN 0100 .
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
save_ok = ok_code .
CLEAR ok_code.
CASE save_ok.
WHEN 'BACK'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form load_data_into_grid
*&---------------------------------------------------------------------*
FORM load_data_into_grid.
SELECT * INTO CORRESPONDING FIELDS OF TABLE itab
FROM ekpo UP TO 4 ROWS.
sla-cwidth_opt = 'X'.
sla-zebra = 'X'.
CALL METHOD go_grid->set_table_for_first_display
EXPORTING
is_layout = sla
CHANGING
it_outtab = itab[]
it_fieldcatalog = gt_fieldcat[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ENDFORM. " load_data_into_grid
*&--------------------------------------------------------------------*
*& Form fieldcat_init
*&--------------------------------------------------------------------*
FORM fieldcat_init .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'ZSTRU001'
CHANGING
ct_fieldcat = gt_fieldcat.
ENDFORM. "fieldcat_init
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'MAIN100'.
IF go_grid IS INITIAL.
CREATE OBJECT go_grid
EXPORTING
i_parent = cl_gui_container=>screen0.
PERFORM fieldcat_init .
PERFORM load_data_into_grid.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT