How to insert and delete column in SAP ALV Display of type CL_SALV_TABLE.

The column is inserted through the field catalog of the ALV Table. It is set during the method call FACTORY ().

Step1: Extend the structure either locally or globally through the Transaction SE11 with the additional field.

TYPES: BEGIN OF ty_contact.

                            INCLUDE STRUCTURE ztt_db_table2.

                            TYPES: contact_age TYPE ZDE_contact_age.

              END OF ty_contact.

DATA lt_contact TYPE STANDARD TABLE OF ty_contact.

              It is always recommended to use an unique Data element for the fields of the structure, so that the column heading text can be easily maintained.

Step2: Call the Factory() Method of class CL_SALV_TABLE

CALL METHOD cl_salv_table => factory

              IMPORTING

                            r_salv_table = lr_alv_object

              CHANGING

                            t_table = lt_contact.

Through the field catalog of the ALV Table, a new column can be inserted or deleted.

There is a method REMOVE_COLUMN() for the column class ‘CL_SALV_COLUMN_TABLE’. But it is protected. Therefore the inheritance is needed to remove the column.

How to insert a new column in to the SAP ALV list

A new column can be inserted in to the SAP ALV list by adjusting the following two entities.

  1. Structure or Type of the table.
  2. New entry in the field catalog internal table.
  • Structure or Type of the Table.

A local type is created with the new column.

              TYPES: BEGIN OF ty_contact.

                                INCLUDE STRUCTURE ztt_db_table2.

                                TYPES: new_contact_address TYPE char0064.

                            END OF ty_contact.

  • Insertion of new column details entry in field catalog.

DATA ls_field_cat TYPE lvc_s_fcat.

ls_field_cat -col_pos = 5.

ls_field_cat -fieldname = ‘NEW_CON_ADD’.

ls_field_cat -inttype = ‘C’.

ls_field_cat -datatype = ‘CHAR’.

ls_field_cat -intlen = 64.

ls_field_cat -scrtext_s= ‘New’.

ls_field_cat -scrtext_m= ‘New Contact’.

ls_field_cat -scrtext_l= ‘New Contact Address’.

APPEND ls_field_cat TO lt_field_cat.