OO ALV: Disable DELETE key on Keyboard in ALV Grid

By | October 16, 2008 | OO ALV | 15,606 | 7

Catch the event generated by DELETE-key on Keyboard & disable it in OO ALV using CL_GUI_ALV_GRID

From ABAP 6.20, SAP has provided many options with the ALV Grid created using the OO ALV using class CL_GUI_ALV_GRID. In the editable ALV, we can insert a new record, delete the record, modify the record and Save those records into database. Ofcourse, we have to write logic more all the process, expect the Delete record. Because records from the ALV grid can be deleted using the DELETE key on the keyboard..!!

Once in a while, we need to restrict this deletion. For this, we need to remove the option of delete from the ALV toolbar. This code will remove the option of Delete as well as the Insert from the toolbar.

*& Code snippet for removing the delete button from the ALV toolbar in
*&   OO ALV created using CL_GUI_ALV 
*  Set the layout option to disable the option to insert or delete a
*  new row in from the input enable ALV
  datals_layout type LVC_S_LAYO.
  ls_layout-NO_ROWINS 'X'.
  call method g_grid->SET_FRONTEND_LAYOUT
      is_layout ls_layout.

But still, records can be deleted with the DELETE key on keyboard. To avoid this we need to disable the DELETE key on the keyboard.

The solution, which I am going to provide will let the user delete the record, but we will give the message to user like “You can not delete…” and restore the data back. In order to achieve implement this solution, we need to:
1. Register the MODIFIED event for the Grid
2. Set the event handler for the events of the Grid DATA_CHANGED and DATA_CHANGED_FINISHED. We need to implement them also to get the deleted rows and insert them back into the main output table.

In the DATA_CHANGED event, we will be able to find if there are any records being deleted. In the DATA_CHANGED_FINISHED, we will set the data back from the temporary output table to main output table and Refresh the output.

Check out the code snippet:
1. Data declaration in the Global section

*& Code snippet for diabling the delete key on the keyboard in the
*&   OO ALV created using CL_GUI_ALV
*1. Declare this data in global:
* My existing table
gt_outtab   TYPE gs_outtab OCCURS WITH HEADER LINE,
* Copy to hold temp data
gt_outtab1  TYPE gs_outtab OCCURS WITH HEADER LINE.  "New copy table
DATAl_deleted TYPE flag.


*& Event Handler Defintion and Implementation
* Defintion of the Events: DATA_CHANGED & DATA_CHANGED_FINISHED.
CLASS lcl_event_receiver DEFINITION.
*    data change event
     handle_data_changed  FOR EVENT data_changed
                          OF cl_gui_alv_grid
                          IMPORTING er_data_changed,
*    data change finished event
     handle_changed_finished FOR EVENT data_changed_finished
                           OF cl_gui_alv_grid
                           IMPORTING e_modified
ENDCLASS.                    "lcl_event_receiver DEFINITION
* Implementation of events
CLASS lcl_event_receiver IMPLEMENTATION.
* This method gives us the data before delete.
* Parameter er_data_changed has the table which has the rows which are
* marked for delete or trying to delete it by pressing the delete button
  METHOD  handle_data_changed.
    DATAls_deleted_rows LIKE LINE OF er_data_changed->mt_deleted_rows.
*   data is deleted or not. If yes, than fill the temporary table to its copy
    DESCRIBE TABLE er_data_changed->mt_deleted_rows LINES sy-index.
    IF sy-index IS NOT INITIAL.
      l_deleted 'X'.
      gt_outtab1[] gt_outtab[].
      CLEAR er_data_changed->mt_deleted_rows.
  ENDMETHOD.                    "handle_data_changed
* data has been deleted than set the temp data back to the main table
* and refresh the table display
  METHOD handle_changed_finished.
*   restore data
    IF l_deleted 'X'.
      gt_outtab[] gt_outtab1[].
      REFRESH gt_outtab1.
*   refresh
    CALL METHOD g_grid->refresh_table_display
        finished 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.
  ENDMETHOD.                    "handle_changed_finished

3. Set Handler for those events before you set the table for display

*& Set Event Handler for the ALV Grid
* Register the method
  CALL METHOD g_grid->register_edit_event
      i_event_id cl_gui_alv_grid=>mc_evt_modified.
* Set event handler
  CREATE OBJECT g_event_receiver.
  SET HANDLER g_event_receiver->handle_data_changed FOR g_grid.
  SET HANDLER g_event_receiver->handle_changed_finished FOR g_grid.

In the next post, we will see how to implement this functionality in the ALV Grid created using the Function module.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments


  • Anonymous

    Perfect, it worked for me!! Thank you very much.

  • Anonymous

    Thanks! It works!

  • Anonymous

    Well, it works, BUT… you have to press enter to get into the the handle_data_changed event.
    So, if you just select a line in your ALV and press the DEL button on the keyboard, the line actually disappears from the grid.
    Is there no other way to not be able to remove the line from the grid with the DEL button ?


  • Anonymous

    Does not work. Thanks a lot.

  • Anonymous

    That is great, but it removes any protocol entries when you refresh the table display. Is there anyway around that?

  • Tra

    Great Help! Thks.

    I want to know one more things.If select all rows and press delete key, can’t catch with data changed event and after clicking 3 times in rowmark column, show deleted information.

    Please let me know if you have any solution for that.

    great Thks.

  • deavy

    Thx! It works!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.