
An Easy Reference for ALV Grid Control
Step
2
Æ
Declare global variables to be used for ALV Grid.
Code Part 1 –
Global data definitions for ALV
*-- Global data definitions for ALV
*--- ALV Grid instance reference
DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid .
*--- Name of the custom control added on the screen
DATA gc_custom_control_name TYPE scrfname VALUE ‘CC_ALV’ .
*--- Custom container instance reference
DATA gr_ccontainer TYPE REF TO cl_gui_custom_container .
*--- Field catalog table
DATA gt_fieldcat TYPE lvc_t_fcat .
*--- Layout structure
DATA gs_layout TYPE lvc_s_layo .
Step
3
Æ
Declare your internal table which is supposed to hold the list data. Let’s name it “gt_list”.
Here is an example declaration.
Code Part 2 –
Declaration of the internal table that will hold the list data
*--- Internal table holding list data
DATA BEGIN OF gt_list OCCURS 0 .
INCLUDE STRUCTURE SFLIGHT .
*--In further sections, some additional fields will added here
*--for some functionality
DATA END OF gt_list .
Step
4
Æ
Somewhere in your program before calling list display, fill your list data as you want. Here,
it is not our concern what the data are. We assume the internal table is filled reasonably. We will use the
data of table
SFLIGHT
as our list data.
Step 5
Æ
Call the screen which comprises the ALV Grid control. At PBO of this screen we will deal
with creating the ALV Grid instance.
Code Part 3 –
PBO of the flow logic for the screen containing ALV Grid control
*--PBO
PROCESS BEFORE OUTPUT .
...
MODULE display_alv .
...
© 2005 SAP AG
6

An Easy Reference for ALV Grid Control
Code Part 4
–
Inside the module
...
MODULE display_alv OUTPUT .
PERFORM display_alv .
ENDMODULE .
Step
6
Æ
Now, it is high time we wrote something to play. So, this piece will be the one we will deal
mainly. What we do is, checking whether an instance of the container (or ALV Grid) exists. If it exists,
refreshing it, and if not, creating and setting ALV for the first display.
FORM display_alv .
IF gr_alvgrid IS INITIAL .
*----Creating custom container instance
CREATE OBJECT gr_ccontainer
EXPORTING
container_name = gc_custom_control_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6 .
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
*----Creating ALV Grid instance
CREATE OBJECT gr_alvgrid
EXPORTING
i_parent = gr_ccontainer
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
others = 5 .
© 2005 SAP AG
7

An Easy Reference for ALV Grid Control
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
*----Preparing field catalog.
PERFORM prepare_field_catalog CHANGING gt_fieldcat .
*----Preparing layout structure
PERFORM prepare_layout CHANGING gs_layout .
*----Here will be additional preparations
*--e.g. initial sorting criteria, initial filtering criteria, excluding
*--functions
CALL METHOD gr_alvgrid->set_table_for_first_display
EXPORTING
* I_BUFFER_ACTIVE =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME =
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
is_layout = gs_layout
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
CHANGING
it_outtab = gt_list[]
it_fieldcatalog = gt_fieldcat
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4 .
© 2005 SAP AG
8

An Easy Reference for ALV Grid Control
Code Part 5 –
Form checking instance existence, creating instance, setting for first display and
refreshing
From ABAP objects, we are familiar with “
CREATE OBJECT
” statement which instantiate classes. In
this snippet of code, we used two instance methods of “
cl_gui_alv_grid
”. First is
“
set_table_for_first_display
” whose name implies for what it is used. After creating the ALV Grid
instance we call this method to make our list displayed. We pass list data table, field catalog table, layout
structure and additional information. Here are parameter definitions taken from SAP Library.
Parameter Meaning
I_BUFFER_ACTIVE
Flag to be set by the application if the method call is static. This
means the method is always called with the same field catalog. In
this case, the field catalog can be held in a special buffer. This
accelerates the display of small lists, in particular.
I_STRUCTURE_NAME
Name of the DDIC structure (for example, 'SFLIGHT') for the
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
ELSE .
CALL METHOD gr_alvgrid->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
EXCEPTIONS
finished = 1
OTHERS = 2 .
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
ENDIF .
ENDFORM .
© 2005 SAP AG
9

An Easy Reference for ALV Grid Control
data in the output table. If you specify this parameter, the field
catalog is generated automatically.
IS_VARIANT
Determines the layout to be used for displaying the output table. If
you use this parameter, you must at least fill field
REPORT
of the
structure of type
DISVARIANT
.
I_SAVE
Determines the options available to the user for saving a layout:
'X': global saving only
'U': user-specific saving only
'A': corresponds to 'X' and 'U'
SPACE: no saving
I_DEFAULT
This parameter determines if the user is allowed to define default
layouts:
'X': Default layouts allowed (default setting)
SPACE: Default layouts not allowed
If default layouts are allowed and if such a layout exists and no
other layout is specified in
IS_VARIANT
, the default layout is
automatically loaded when this method is called.
IS_LAYOUT
Determines properties of the grid control. The layout structure has
nothing
to do with the layout for saving filter, sort, and column
properties.
IS_PRINT
Parameter for printing on the backend
IT_SPECIAL_GROUPS
If in the field catalog the columns were grouped together with
field
SP_GROUP
, you must pass a table with texts for these
groups. On the current layout window, it is then possible to use a
list box to restrict column selection to one of these groups.
IT_TOOLBAR_EXCLUDING
This table contains function codes of the toolbar that you want to
hide for the lifetime of the ALV Grid Control. The function codes
are constant attributes and are prefixed with
MC_FC_
.
IT_HYPERLINK
This table assigns a hyperlink address (field
HREF
of
LVC_S_HYPE
) to each handle (field
HANDLE
of
LVC_S_HYPE
).
Using this handle, you can then include hyperlinks in the grid.
IT_ALV_GRAPHICS
Settings for displaying the ALV list as a diagram (for example,
axis labels). The row type of the table has two fields
(variables/value pairs):
© 2005 SAP AG
10