Monday, December 1, 2008

ALV grid control in 3 lines

Here is some code inspired by Dany Charbonneau (link). It creates an ALV grid control with just 3 lines of ABAP. You should not be using REUSE_ALV_GRID_DISPLAY or REUSE_ALV_GRID_DISPLAY_LVC function modules any more - these are superseded by programming the objects directly. As you move over to ABAP Web Dynpro you will need those object skills anyway...

I like the elegance of well designed and documented classes. But I hate abstraction for its own sake - simple structured programs and reports are quicker to develop and easier to maintain. If you are not expecting to reuse the class then there is a strong argument for not writing it in the first place.

Fortunately CL_GUI_ALV_GRID is one of the well designed and documented ones. Here is the program.



report zzzzzzz1.

data:
lo_alv type ref to cl_gui_alv_grid,
lt_tab type table of t001.

selection-screen begin of screen 1100.
selection-screen end of screen 1100.

select * from t001 into table lt_tab.

*-- Line 1 - instantiate the alv object in the required screen
create object lo_alv
exporting
i_parent = cl_gui_container=>screen0.

*-- Line 2 - specify the data structure and content
call method lo_alv->set_table_for_first_display
exporting
i_structure_name = 'T001'
changing
it_outtab = lt_tab.

*-- Line 3 - display the screen
call selection-screen 1100.

1 comment:

Lunatic said...

Epic! Just a small suggestion: For ALV output I would suggest using Screen 1000 instead of 1100. Since its the standard screen output for selection screen.