
An Easy Reference for ALV Grid Control
Pushbuttons On The List
To make a cell to be displayed as a pushbutton, we have two steps. Firstly, insert a new inner table of
type “
LVC_T_STYL
” into your list data table.
Code Part 25 –
Inserting an inner table to store cell display styles
*--- Internal table holding list data
DATA BEGIN OF gt_list OCCURS 0 .
INCLUDE STRUCTURE SFLIGHT .
DATA rowcolor(4) TYPE c .
DATA cellcolors TYPE lvc_t_scol .
DATA carrid_handle TYPE int4 .
DATA connid_handle TYPE int4 .
DATA cellstyles TYPE lvc_t_styl .
DATA END OF gt list .
Fill this inner table for each field to be displayed as pushbutton.
Code Part 26 -
A sample code to make the cell at row 7 and column ‘SEATSMAX’ displayed as
pushbutton
DATA ls_style TYPE lvc_s_styl .
...
READ TABLE gt_list INDEX 7 .
ls_style-fieldname = 'SEATSMAX' .
ls_style-style = cl_gui_alv_grid=>mc_style_button .
APPEND ls_style TO gt_list-cellstyles .
MODIFY gt_list INDEX 7 .
As usual, we state our list data table field related with styles in the layout structure at field
‘
STYLEFNAME
’.
e.g.
ps_layout-stylefname = 'CELLSTYLES' .
Button click event is handled like hotspot click via the event “
button_click
” through its parameters
“
es_col_id
” and “
es_row_no
” which contain the address of the clicked pushbutton cell.
© 2005 SAP AG
41

An Easy Reference for ALV Grid Control
Adding Your Own Functions
ALV Grid control has an open door letting you to add your own functions triggered by a button press
on the ALV toolbar. For this, we mainly utilize two of ALV Grid events. We use the event “toolbar” to add
the button and the event “
user_command
” to implement the new function.
In the method handling the “toolbar” event, we define a new button by filling a structure and
appending it to the table attribute “
mt_toolbar
” of the object to whose reference we can reach via the
parameter “
e_object
” of the event.
FORM handle_toolbar USING i_object TYPE REF TO cl_alv_event_toolbar_set .
DATA: ls_toolbar TYPE stb_button.
CLEAR ls_toolbar.
MOVE 3 TO ls_toolbar-butn_type.
APPEND ls_toolbar TO i_object->mt_toolbar.
CLEAR ls_toolbar.
MOVE 'PER' TO ls_toolbar-function. "#EC NOTEXT
MOVE icon_display_text TO ls_toolbar-icon.
MOVE 'Passenger Info'(201) TO ls_toolbar-quickinfo.
MOVE 'Passenger Info'(201) TO ls_toolbar-text.
MOVE ' ' TO ls_toolbar-disabled. "#EC NOTEXT
APPEND ls_toolbar TO i_object->mt_toolbar.
CLEAR ls_toolbar.
MOVE 'EXCH' TO ls_toolbar-function. "#EC NOTEXT
MOVE 2 TO ls_toolbar-butn_type.
MOVE icon_calculation TO ls_toolbar-icon.
MOVE 'Payment in Other Currencies'(202) TO ls_toolbar-quickinfo.
MOVE ' ' TO ls_toolbar-text.
MOVE ' ' TO ls_toolbar-disabled. "#EC NOTEXT
APPEND ls_toolbar TO i_object->mt_toolbar.
ENDFORM .
Code Part 27 –
Filling the structure for two new buttons
© 2005 SAP AG
42

An Easy Reference for ALV Grid Control
The fields of the structure we fill are as follows:
Field Description
FUNCTION
The function code for the function
BUTN_TYPE
Button type that will be added to the toolbar. Available button
types are:
0 Button
(normal)
1
Menu and default button
2 Menu
3 Separator
4 Radio
button
5 Checkbox
6 Menu
entry
ICON
Icon for the button (optional)
TEXT
Text for the button (optional)
QUICKINFO
Quick info for the button (optional)
DISABLED
Adds the button as disabled
Table 14 –
Fields of structure to be filled to add a new function
In
the
Code Part 22
, we are adding a separator line and two buttons one of which is a normal button
whereas the other is a menu button. To handle a menu button which as added by choosing ‘1’ or ‘2’ as
the button type, we must also implement some coding at the method handling the event “
menu_button
”
to define functions as to be subentries. The functions of these subentries are also handled under the
event “
user_command
”.
FORM handle_menu_button USING i_object TYPE REF TO cl_ctmenu
i_ucomm TYPE syucomm .
CASE i_ucomm .
WHEN 'EXCH' .
CALL METHOD i_object->add_function
EXPORTING
fcode = 'EU'
text = 'Euro' .
CALL METHOD i_object->add_function
EXPORTING
fcode = 'TRL'
text = 'Turkish Lira' .
.. ..
ENDCASE .
ENDFORM. " handle_menu_button
Code Part 28 –
Adding two functions to be subentries for the menu button with function code ‘EXCH’
Now, to implement what to be executed when our button is pressed or our subentry is selected we
need to program our functions in the method handling the event “
user_command
”.
© 2005 SAP AG
43

An Easy Reference for ALV Grid Control
Code Part 29 –
Implementing functioning codes for new functions
FORM handle_user_command USING i_ucomm TYPE syucomm .
DATA lt_selected_rows TYPE lvc_t_roid .
DATA ls_selected_row TYPE lvc_s_roid .
CALL METHOD gr_alvgrid->get_selected_rows
IMPORTING
et_row_no = lt_selected_rows .
READ TABLE lt_selected_rows INTO ls_selected_row INDEX 1 .
IF sy-subrc ne 0 .
MESSAGE s000(su) WITH 'Select a row!'(203) .
ENDIF .
CASE i_ucomm .
WHEN 'CAR' .
READ TABLE gt_list INDEX ls_selected_row-row_id .
IF sy-subrc = 0 .
CALL FUNCTION 'ZDISPLAY_CARRIER_INFO'
EXPORTING carrid = gt_list-carrid
EXCEPTIONS carrier_not_found = 1
OTHERS = 2.
IF sy-subrc NE 0 .
*--Exception handling
ENDIF .
ENDIF .
WHEN 'EU' .
READ TABLE gt_list INDEX ls_selected_row-row_id .
IF sy-subrc = 0 .
CALL FUNCTION 'ZPOPUP_CONV_CURR_AND_DISPLAY'
EXPORTING monun = 'EU'
quant = gt_list-paymentsum.
ENDIF .
.. ..
ENDCASE .
ENDFORM .
© 2005 SAP AG
44

An Easy Reference for ALV Grid Control
As you can see, we are using the method “
get_selected_rows
” to get which row is selected.
Since the button with function ‘
EXCH
’ branches into subfunctions, we do not implement anything for it.
Instead, we implement functional codes for subfunctions.
After all, to make ALV show our additional buttons, we must call the method
“
set_toolbar_interactive
” for the ALV Grid instance after the instance is created.
e.g.
CALL METHOD gr_alvgrid->set_toolbar_interactive .
Overriding Standard Functions
The ALV Grid control also gives the opportunity to override its standard functions. For this purpose,
we utilize the event “
before_user_command
” and the method which sets ALV user command, called
“
set_user_command
”. At “
before_user_command
”, you control the user command to process your
own function and then use the method “
set_user_command
” to set the ALV Grid user command to
space to avoid ALV Grid execute further with the standard function.
FORM handle_before_user_command USING i_ucomm TYPE syucomm .
CASE e_ucomm .
WHEN '&INFO' .
CALL FUNCTION 'ZSFLIGHT_PROG_INFO' .
CALL METHOD gr_alvgrid->set_user_command
EXPORTING i_ucomm = space.
ENDCASE .
ENDFORM .
Code Part 30 –
Overriding the standard ALV Grid function ‘&INFO’
Context Menus
The event related with context menus is “
context_menu_request
”. When you right click on the
ALV Grid area the event-handling method is triggered. Under this method you can add entries in the
context menu. You can utilize “
GET_SELECTED…
” methods to retrieve which cell, row or column the user
has right-clicked. Adding functions to the context menu is accomplished as in adding subfunctions to non-
standard menu buttons as described in
section
“Adding Your Own Functions”
.
© 2005 SAP AG
45