CL_GUI_ALV_GRID¶
The editable ALV grid — use this when you need users to change values inline; use CL_SALV_TABLE for read-only display.
Purpose¶
CL_GUI_ALV_GRID is the older but still essential ALV class for scenarios requiring editable cells. Unlike CL_SALV_TABLE, it requires a custom container on a screen (module pool) and cannot be called from a simple report with a single line. In return it supports:
- Editable cells with validation
- Cell-level colours and icons
- Row selection with callbacks
- Totals rows
- Custom toolbar buttons
A screen and container are required
CL_GUI_ALV_GRID cannot display anything without a CL_GUI_CUSTOM_CONTAINER placed on a screen via the Screen Painter (SE51). You must create a module pool program with at least one screen (e.g. screen 100) that has a custom control area named CC_ALV. See the example below.
Minimum working example¶
How to set it up in SE80:
- Create a module pool program (e.g.
ZSALV_GRID_DEMO, typeM) - Create screen 100 via the Screen Painter (double-click on
100in the navigation tree) - In the screen layout, insert a Custom Control element named
CC_ALV(fills the screen) - Create a PBO module
pbo_100and PAI modulepai_100 - Paste the code below into the respective includes
Global data (top include):
DATA: go_container TYPE REF TO cl_gui_custom_container,
go_alv TYPE REF TO cl_gui_alv_grid,
gt_data TYPE STANDARD TABLE OF sflight.
Call screen from START-OF-SELECTION:
PBO module (screen 100 — Process Before Output):
You must create the PF-status manually — use a Z/Y name
SET PF-STATUS 'ZMAIN' references a local GUI status in your module pool. Use a Z or Y prefix for your status name (e.g. ZMAIN). The easiest way to create it is to copy the SAP standard status: open SE41, enter program SAPLSALV and status STANDARD, then Status → Copy and set your program and ZMAIN as the target. If SAPLSALV is not available in your system, try SAPLKKBL instead. Without a PF-status the screen opens with no function keys.
The PF-status only serves one purpose here: providing the navigation keys &F03 (Back), &F15 (Exit), &F12 (Cancel) so the user can close the screen. Custom ALV actions do not go through the PF-status — they use the ON_TOOLBAR and ON_USER_COMMAND events instead (see below).
MODULE pbo_100 OUTPUT.
SET PF-STATUS 'ZMAIN'.
SET TITLEBAR 'TITLE'.
IF go_container IS INITIAL.
" Create container — 'CC_ALV' must match the custom control name in Screen Painter
CREATE OBJECT go_container
EXPORTING container_name = 'CC_ALV'.
" Create ALV grid inside the container
CREATE OBJECT go_alv
EXPORTING i_parent = go_container.
" Load data
SELECT * FROM sflight INTO TABLE @gt_data UP TO 50 ROWS.
" Build field catalog automatically from DDIC structure
DATA lt_fcat TYPE lvc_t_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = lt_fcat.
" Display the grid
go_alv->set_table_for_first_display(
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = gt_data ).
ELSE.
" Refresh after data change
go_alv->refresh_table_display( ).
ENDIF.
ENDMODULE.
PAI module (screen 100 — Process After Input):
MODULE pai_100 INPUT.
CASE sy-ucomm.
WHEN '&F03' OR '&F15' OR '&F12'. " Back / Exit / Cancel
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
Adding a Save button
The standard status copied from SAPLSALV does not include a Save function. To add one, open your status in SE41, add a new function code (e.g. SAVE) to a toolbar button or menu entry, then handle it in your PAI module:
Make cells editable¶
To allow users to edit specific columns, set edit = abap_true on the relevant field catalog entry and pass the catalog via it_fieldcatalog. You cannot use i_structure_name at the same time — replace the set_table_for_first_display call in your PBO with this version:
IF go_container IS INITIAL.
CREATE OBJECT go_container
EXPORTING container_name = 'CC_ALV'.
CREATE OBJECT go_alv
EXPORTING i_parent = go_container.
SELECT * FROM sflight INTO TABLE @gt_data UP TO 50 ROWS.
DATA lt_fcat TYPE lvc_t_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = lt_fcat.
" Make SEATSOCC editable
LOOP AT lt_fcat ASSIGNING FIELD-SYMBOL(<ls_fcat>) WHERE fieldname = 'SEATSOCC'.
<ls_fcat>-edit = abap_true.
ENDLOOP.
" Pass it_fieldcatalog explicitly — do NOT use i_structure_name when passing a field catalog
go_alv->set_table_for_first_display(
CHANGING
it_fieldcatalog = lt_fcat
it_outtab = gt_data ).
ELSE.
go_alv->refresh_table_display( ).
ENDIF.
Get selected rows with a custom toolbar button¶
Custom buttons added to the PF-status appear in the screen application toolbar but are hidden by the ALV grid's own toolbar that renders inside the container. To add a button that actually appears in the ALV toolbar you must register the ON_TOOLBAR and ON_USER_COMMAND events.
Step 1 — Add the event handler class to the global data section of your module pool:
DATA: go_container TYPE REF TO cl_gui_custom_container,
go_alv TYPE REF TO cl_gui_alv_grid,
gt_data TYPE STANDARD TABLE OF sflight.
CLASS lcl_events DEFINITION.
PUBLIC SECTION.
METHODS:
on_toolbar
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
on_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS.
CLASS lcl_events IMPLEMENTATION.
METHOD on_toolbar.
DATA ls_button TYPE stb_button.
ls_button-function = 'ZSHOW'.
ls_button-icon = icon_display.
ls_button-text = 'Show Selected'.
ls_button-butn_type = 0. " normal button
APPEND ls_button TO e_object->mt_toolbar.
ENDMETHOD.
METHOD on_user_command.
DATA lt_selected TYPE lvc_t_row.
DATA lv_info TYPE string.
DATA ls_row TYPE sflight.
CASE e_ucomm.
WHEN 'ZSHOW'.
go_alv->get_selected_rows( IMPORTING et_index_rows = lt_selected ).
IF lt_selected IS INITIAL.
MESSAGE 'No rows selected — click a row first' TYPE 'S' DISPLAY LIKE 'W'.
ELSE.
LOOP AT lt_selected INTO DATA(ls_sel).
READ TABLE gt_data INTO ls_row INDEX ls_sel-index.
IF sy-subrc = 0.
lv_info = lv_info &&
|Row { ls_sel-index }: { ls_row-carrid } / { ls_row-connid } / Price: { ls_row-price }| &&
cl_abap_char_utilities=>newline.
ENDIF.
ENDLOOP.
MESSAGE lv_info TYPE 'I'.
ENDIF.
ENDCASE.
ENDMETHOD.
ENDCLASS.
Step 2 — Register the event handlers in your PBO after creating go_alv:
IF go_container IS INITIAL.
CREATE OBJECT go_container EXPORTING container_name = 'CC_ALV'.
CREATE OBJECT go_alv EXPORTING i_parent = go_container.
" Register events before set_table_for_first_display
DATA lo_events TYPE REF TO lcl_events.
lo_events = NEW lcl_events( ).
SET HANDLER lo_events->on_toolbar FOR go_alv.
SET HANDLER lo_events->on_user_command FOR go_alv.
SELECT * FROM sflight INTO TABLE @gt_data UP TO 50 ROWS.
go_alv->set_table_for_first_display(
EXPORTING i_structure_name = 'SFLIGHT'
CHANGING it_outtab = gt_data ).
ELSE.
go_alv->refresh_table_display( ).
ENDIF.
Select one or more rows in the grid using the row selector on the left, then click Show Selected in the ALV toolbar. An info popup lists each selected row with its carrier, connection, and price.
Add row colouring¶
Row colouring requires three things: a colour field in your data type, a hidden field catalog entry for it, and info_fname set in the layout. Because you need a custom type and a manual field catalog, this replaces the simple i_structure_name approach from the minimum example.
Color code format — the colour field is TYPE c LENGTH 4, value format CXYZ:
| Position | Meaning |
|---|---|
C |
literal prefix |
X |
color number (1–7) |
Y |
intensified: 0 = normal, 1 = bold |
Z |
inverse: 0 = normal, 1 = inverse |
Common values: 'C500' = green, 'C600' = red, 'C300' = yellow, 'C100' = blue.
Step 1 — Update the global data section to use a custom type:
TYPES: BEGIN OF ty_flight,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
price TYPE sflight-price,
currency TYPE sflight-currency,
rowcolor TYPE c LENGTH 4,
END OF ty_flight.
DATA: go_container TYPE REF TO cl_gui_custom_container,
go_alv TYPE REF TO cl_gui_alv_grid,
gt_data TYPE STANDARD TABLE OF ty_flight.
Step 2 — PBO module with field catalog and colour logic:
MODULE pbo_100 OUTPUT.
SET PF-STATUS 'ZMAIN'.
SET TITLEBAR 'TITLE'.
IF go_container IS INITIAL.
CREATE OBJECT go_container EXPORTING container_name = 'CC_ALV'.
CREATE OBJECT go_alv EXPORTING i_parent = go_container.
" Load data into custom type
SELECT carrid, connid, price, currency
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE @gt_data
UP TO 50 ROWS.
" Colour rows: green for price < 500, red for price >= 500
LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<ls>).
<ls>-rowcolor = COND #( WHEN <ls>-price < 500 THEN 'C500' ELSE 'C600' ).
ENDLOOP.
" Build field catalog manually — tabname pulls DDIC column headers automatically
DATA lt_fcat TYPE lvc_t_fcat.
DATA ls_fc TYPE lvc_s_fcat.
ls_fc-fieldname = 'CARRID'. ls_fc-tabname = 'SFLIGHT'.
APPEND ls_fc TO lt_fcat. CLEAR ls_fc.
ls_fc-fieldname = 'CONNID'. ls_fc-tabname = 'SFLIGHT'.
APPEND ls_fc TO lt_fcat. CLEAR ls_fc.
ls_fc-fieldname = 'PRICE'. ls_fc-tabname = 'SFLIGHT'.
APPEND ls_fc TO lt_fcat. CLEAR ls_fc.
ls_fc-fieldname = 'CURRENCY'. ls_fc-tabname = 'SFLIGHT'.
APPEND ls_fc TO lt_fcat. CLEAR ls_fc.
ls_fc-fieldname = 'ROWCOLOR'. ls_fc-no_out = abap_true. " hidden — users never see it
APPEND ls_fc TO lt_fcat. CLEAR ls_fc.
" Layout: tell ALV which field contains the row colour code
DATA ls_layout TYPE lvc_s_layo.
ls_layout-info_fname = 'ROWCOLOR'.
go_alv->set_table_for_first_display(
EXPORTING
is_layout = ls_layout
CHANGING
it_fieldcatalog = lt_fcat
it_outtab = gt_data ).
ELSE.
go_alv->refresh_table_display( ).
ENDIF.
ENDMODULE.
Using row colouring together with the Get selected rows example
Switching gt_data to TYPE STANDARD TABLE OF ty_flight changes the row type. If you also have the on_user_command handler from the previous section, update DATA ls_row TYPE sflight to DATA ls_row TYPE ty_flight there — otherwise activation will fail with: The row types of "GT_DATA" and "LS_ROW" cannot be converted to each other.
Further options¶
CL_GUI_ALV_GRID has many more capabilities — each typically involves a separate class or event registration:
| Feature | How |
|---|---|
| Custom toolbar buttons | Register on_toolbar event, use cl_gui_alv_grid=>mc_fc_* constants |
| Cell-level colour | Use lvc_t_scol column in data table |
| Double-click row event | Register on_double_click event |
| Row-level icons | Add icon_d type column, set icon constants |
| Subtotals / grouping | Set subtot in field catalog, define sort criteria |
CL_SALV_TABLE vs CL_GUI_ALV_GRID
Use CL_SALV_TABLE for read-only grids — it is simpler and requires no screen. Use CL_GUI_ALV_GRID only when you need editable cells, cell colouring with icons, or custom toolbar buttons.
See also¶
CL_SALV_TABLE— simpler read-only ALVCL_SALV_FUNCTIONS_LIST— toolbar button configuration (SALV)