SAP ALV Tutorial – Handling Data Change
In previous article, we have already set alv editable, but we still need to validate the input data as well as save the input data to the database. In another case, we also need to change according to user’s event.
As we can now make our alv Grid editable we may require controlling input data. The alv Grid has events data_changed and data_changed_finished. The former method is triggered just after the change at an editable field is perceived. Here you can make checks for the input. And the second event is triggered after the change is committed. You can select the way how the control perceives data changes by using the method register_edit_event. You have two choices:
1. After return key is pressed: To select this way, to the parameter event_id pass cl_gui_alv_grid=>mc_evt_enter.
2. After the field is modified and the cursor is moved to another field: For this, pass cl_gui_alv_grid=>mc_evt_modifies to the same parameter.
To make events controlling data changes be triggered, you must select either way by calling this method. Otherwise, these events will not be triggered.
To control field data changes, alv Grid uses an instance of the class “CL_ALV_CHANGED_DATA_PROTOCOL” and passes this via the event data_changed. Using methods of this class, you can get and modify cell values and produce error messages.
Utilizing these methods and attributes you can check and give proper message and also modify the cell content.
FORM handle_data_changed USING ir_data_changed TYPE REF TO cl_alv_changed_data_proto DATA : ls_mod_cell TYPE lvc_s_modi , lv_value TYPE lvc_value . SORT ir_data_changed->;mt_mod_cells BY row_id . LOOP AT ir_data_changed->;mt_mod_cells INTO ls_mod_cell WHERE fieldname = 'SEATSMAX' . CALL METHOD ir_data_changed->;get_cell_value EXPORTING i_row_id = ls_mod_cell-row_id i_fieldname = 'CARRID' IMPORTING e_value = lv_value . IF lv_value = 'THY' AND ls_mod_cell-value >; '500' . CALL METHOD ir_data_changed->;add_protocol_entry EXPORTING i_msgid = 'SU' i_msgno = '000' i_msgty = 'E' i_msgv1 = 'This number can not exceed 500 for ' i_msgv2 = lv_value i_msgv3 = 'The value is et to "500'" i_fieldname = ls_mod_cell-fieldname i_row_id = ls_mod_cell-row_id . CALL METHOD ir_data_changed->;modify_cell EXPORTING i_row_id = ls_mod_cell-row_id i_fieldname = ls_mod_cell-fieldname i_value = '500' . ENDIF . ENDLOOP . ENDFORM. " handle_data_changed