ALV grid is normally used to display the report/program output. But there are cases where you need edit functionality in ALV grid.

Having a editable column/ field is very simple in ALV grid. During field catalog population just set the edi property of the column.



 wa_fieldcat-edit        = 'X'.

Now , Not so easy task is to code the logic to save the data after edit operation.

We have to use events for the same.
i_callback_user_command        =  gt_callback_subroutine
is used to activate a subroutine 'USER_COMMAND' which will be triggered on each user action on ALV (Mouse click , Button click etc).We need to code in this subroutine. 

SAVE button is having a function code '&DATA_SAVE'. So we need to check if sy-ucom = '&DATA_SAVE'.

Whenever any edit is done in ALV grid , the same is reflected back in the internal table used. So we can use the latest contents of internal table to update/ do our operations. To get only those fields which are modified , better to copy the internal table to some temp table and compare each row with the new contents.

SO, here is the program incorporating all things explained.




REPORT zau_alvgridtest.
TYPE-POOLS:icon. " Just include this
TYPES: BEGIN OF ty_matnr,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
mtart TYPE mara-mtart,
status TYPE char4, " Reserve a field for traffic lights
rowcolor(4) TYPE c, "row colour
END OF ty_matnr.

TYPE-POOLS : slis.
TABLES: mara.

DATA : wa_fieldcat TYPE slis_fieldcat_alv, "workspace
it_fieldcat TYPE slis_t_fieldcat_alv, " Table
it_mara TYPE STANDARD TABLE OF ty_matnr,
it_maracp TYPE STANDARD TABLE OF ty_matnr,
it_changes TYPE STANDARD TABLE OF ty_matnr,
wa_mara TYPE ty_matnr,
wa_maracp TYPE ty_matnr,
g_top_of_page TYPE slis_formname VALUE 'F_TOP_OF_PAGE', "for avl header.
it_layout TYPE slis_layout_alv,
gt_callback_subroutine TYPE slis_formname VALUE 'USER_COMMAND',
lv_run TYPE i VALUE '0'.


SELECT-OPTIONS : s_matnr FOR mara-matnr.

PARAMETERS: rd1 RADIOBUTTON GROUP rb DEFAULT 'X',
rd2 RADIOBUTTON GROUP rb .

START-OF-SELECTION.
IF s_matnr IS NOT INITIAL.
SELECT matnr ersda
ernam mtart
FROM mara
INTO TABLE it_mara
WHERE matnr IN s_matnr.

IF it_mara IS NOT INITIAL.
LOOP AT it_mara INTO wa_mara.

IF lv_run EQ 0.
wa_mara-rowcolor = 'C300'.
wa_mara-status = icon_red_light. "Update the traffic lights as you wish, just go through the type pool you will find many colors and icons

MODIFY it_mara FROM wa_mara.
lv_run = 1.
ELSE.
wa_mara-rowcolor = 'C600'.
wa_mara-status = icon_green_light.
MODIFY it_mara FROM wa_mara .
lv_run = 0.
ENDIF.
ENDLOOP.
PERFORM f_build_catalog.
IF rd1 EQ 'X'.

* it_layout-info_fieldname = 'ROWCOLOR'.
PERFORM f_display_grid.
ELSE.
* it_layout-info_fieldname = 'ROWCOLOR'.
PERFORM f_display_list.
ENDIF.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form f_build_catalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_build_catalog.
wa_fieldcat-fieldname = 'MATNR'.
wa_fieldcat-seltext_m = 'Material Value'.
wa_fieldcat-col_pos = 1.
wa_fieldcat-key = 'X'.

wa_fieldcat-hotspot = 'X'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.

wa_fieldcat-fieldname = 'ERSDA'.
wa_fieldcat-seltext_m = 'Created On'.
wa_fieldcat-edit = 'X'.
wa_fieldcat-col_pos = 2.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.

wa_fieldcat-fieldname = 'ERNAM'.
wa_fieldcat-seltext_m = 'Created by'.
wa_fieldcat-edit = 'X'.
wa_fieldcat-col_pos = 3.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.

wa_fieldcat-fieldname = 'MTART'.
wa_fieldcat-seltext_m = 'Material Type'.
wa_fieldcat-edit = 'X'.
wa_fieldcat-col_pos = 4.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.

wa_fieldcat-fieldname = 'STATUS'.
wa_fieldcat-seltext_m = 'Status'.
wa_fieldcat-icon = 'X'. " Display the field as ICON
wa_fieldcat-col_pos = 5.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.



ENDFORM. "f_build_catalog

*&---------------------------------------------------------------------*
*& Form f_display_grid
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_display_grid.
it_maracp[] = it_mara[].
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
i_callback_program = sy-repid
* I_CALLBACK_PF_STATUS_SET = ' '
i_callback_user_command = gt_callback_subroutine
i_callback_top_of_page = g_top_of_page
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
is_layout = it_layout
it_fieldcat = it_fieldcat
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_STARlv_line = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP = 0
* I_HTML_HEIGHT_END = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = it_mara
EXCEPTIONS
program_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. "f_display_grid
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_UCOMM text
* -->P_SELFIELD text
*----------------------------------------------------------------------*
FORM user_command USING p_ucomm LIKE sy-ucomm
p_selfield TYPE slis_selfield.
"p_ucomm will hold user action like double click, clicking a button ,etc
CASE p_ucomm.
WHEN '&IC1'. " SAP standard code for double-clicking
READ TABLE it_mara INTO wa_mara INDEX p_selfield-tabindex. " Getting Row data
CASE p_selfield-fieldname.
WHEN 'MATNR'. " Column data
SET PARAMETER ID: 'MAT' FIELD p_selfield-value.

CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
WHEN 'MTART'.
MESSAGE p_selfield-value TYPE 'S'.
ENDCASE.
WHEN
PERFORM f_save_data.
MESSAGE 'Trying to save' TYPE 'S'.
"It_changes hold all the change made in the ALV
ENDCASE.
ENDFORM. "user_command
*&---------------------------------------------------------------------*
*& Form f_save_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_save_data.
clear it_changes[].
LOOP AT it_mara INTO wa_mara.
READ TABLE it_maracp INTO wa_maracp index sy-tabix.
IF wa_maracp NE wa_mara.
APPEND wa_mara TO it_changes.
ENDIF.
clear wa_maracp.
ENDLOOP.
ENDFORM. "f_save_data
*&---------------------------------------------------------------------*
*& Form f_display_list
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_display_list.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE = ' '
i_callback_program = sy-cprog
* I_CALLBACK_PF_STATUS_SET = ' '
i_callback_user_command = gt_callback_subroutine
* I_STRUCTURE_NAME =
is_layout = it_layout
it_fieldcat = it_fieldcat
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_STARlv_line = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IR_SALV_LIST_ADAPTER =
* IT_EXCEPT_QINFO =
* I_SUPPRESS_EMPTY_DATA = ABAP_FALSE
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = it_mara
EXCEPTIONS
program_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM. "f_display_list

*&---------------------------------------------------------------------*
*& Form f_top_of_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_top_of_page.

DATA: it_header TYPE slis_t_listheader,
wa_header TYPE slis_listheader,
lv_line LIKE wa_header-info,
ld_lines TYPE i,
ld_linesc(10) TYPE c.

* Title
wa_header-typ = 'H'.
wa_header-info = 'MARA Table Report'.
APPEND wa_header TO it_header.
CLEAR wa_header.

* Date
wa_header-typ = 'S'.
wa_header-key = 'Date: '.
CONCATENATE sy-datum+6(2) '.'
sy-datum+4(2) '.'
sy-datum(4) INTO wa_header-info. "todays date
APPEND wa_header TO it_header.
CLEAR: wa_header.

* Total No. of Records Selected
DESCRIBE TABLE it_mara LINES ld_lines.
ld_linesc = ld_lines.
CONCATENATE 'Total No. of Records Selected: ' ld_linesc
INTO lv_line SEPARATED BY space.
wa_header-typ = 'A'.
wa_header-info = lv_line.
APPEND wa_header TO it_header.
CLEAR: wa_header, lv_line.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = it_header.
.
CLEAR it_header.
ENDFORM. "TOP-OF-PAGE


Serkan AKKAVAK
Computer Engineer
serkurumsal@yandex.com