Recently I got a requirement in our project to write a maintenance program for a table. We decided to go with editable ALV grid, with options like Insert, Delete and change rows. It was coming up well, but I was stuck at a point wherein the deletion of row needs to be done based on some conditions. As per the standard ALV functionality, the delete button on the standard tool bar deletes a row and then triggers the DATA_CHANGED event. Since the DATA_CHANGED event gets triggered after the deletion, the deleted row is not tracked in the DATA_CHANGED event and I was unable to trace the deleted entry. I tried finding the solution in SDN, but was in vain, as I ended up finding some unanswered posts.
Here are some posts:
[CL_GUI_ALV_GRID, Delete lines -> check before doing | CL_GUI_ALV_GRID, Delete lines -> check before doing]
[ALV: validation of a deleted row | ALV: validation of a deleted row]
The condition in the requirement is: If the flight date exists, a pop-up message needs to be raised with the message, ‘Record can not be deleted!!!’ else, the record could be deleted.
How to Do........
1. First we need to handle the event DATA_CHANGED in our local class.
METHODS:
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed
e_ucomm.
2. Declaration of output table should be
* Output table declarations
Types: begin of tp_outtab.
include structure sflight.
TYPES: handle_style TYPE lvc_t_styl,
end of tp_outtab.
Data : gt_outtab type standard table of tp_outtab.
3. After populating output table we should modify the records with the below attribute
based on condition. In our scenario if flight date exists then we should not allow the
user to delete the row.
To do that we should set the attribute CL_GUI_ALV_GRID=>MC_STYLE_NO_DELETE_ROW
to the corresponding rows.
LOOP AT GT_OUTTAB INTO LS_OUTTAB.
CLEAR LS_OUTTAB-HANDLE_STYLE.
CLEAR : LS_HANDLE_STYLE.
L_INDEX = SY-TABIX.
REFRESH LT_HANDLE_STYLE.
IF NOT LS_OUTTAB-FLDATE IS INITIAL.
LS_HANDLE_STYLE-STYLE = CL_GUI_ALV_GRID=>
MC_STYLE_NO_DELETE_ROW.
INSERT LS_HANDLE_STYLE INTO TABLE LT_HANDLE_STYLE.
INSERT LINES OF LT_HANDLE_STYLE INTO TABLE LS_OUTTAB-HANDLE_STYLE.
MODIFY GT_OUTTAB FROM LS_OUTTAB INDEX L_INDEX.
ENDIF.
ENDLOOP.
* Layout
GS_LAYOUT-STYLEFNAME = 'HANDLE_STYLE'.
4. Register the events to trigger DATA_CHANGED event when modify or press enter.
* Optionally register ENTER to raise event DATA_CHANGED.
CALL METHOD OBJ_GRID->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER.
CALL METHOD OBJ_GRID->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.
5. In
HANDLE_DATA_CHANGED
method we need to validate this.
*§ 3.Implement your event handler methods. Use WRITE to provide output
*----
METHOD HANDLE_DATA_CHANGED.
*- check the deletion condition
CALL METHOD CHECK_DELETE_COND( ER_DATA_CHANGED ).
ENDMETHOD. " handle_data_changed
*----



