The primary key fields of the data records must be unique. There should not be duplicate records in the Database table. The fields of the ALV List are editable through the field catalog. The primary key fields of the SAP ALV List cannot be edited directly. If a new line of record is inserted in to SAP ALV List, the primary key fields of the SAP ALV List are disabled by default. It is not possible to insert a new record in the ALV List through the edit field of the field catalog.
Contact_Id | Contact_name | Contact_address |
101 | Ram | Street 10 |
102 | Mahes | Street 11 |
If a new line of record is inserted in to above SAP ALV List, the field contact_id is disabled by default. The contact_id is the primary key field of the SAP ALV List. If a new line of record must be inserted in the Table, then the primary key fields of the SAP ALV List must be enabled for editing. The primary key fields of the existing data records must be disabled by default.
A new line of record can be inserted in to the ALV Table through the internal table lvc_t_styl. It is introduced to the structure of the internal table of data records. The keywords ‘INCLUDE STRUCTURE’ adds the existing structure of internal table which contains the data records. The following steps helps us to achieve this.
- Define the structure with lvc_t_styl.
- Define the stylename field of the layout
- Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
- Define the structure with lvc_t_styl.
TYPES: BEGIN OF st_contact.
INCLUDE STRUCTURE ztt_db_table2.
TYPES: fieldstyle TYPE lvc_t_styl,
END OF st_contact.
DATA lt_contact TYPE TABLE OF st_contact.
- Define the stylename field of the layout
DATA ls_layout TYPE lvc_s_layo.
ls_layout-stylename = ‘FIELDSTYLE’.
This layout is given as parameter while calling the set_table_for_first_display method of the ALV object.
- Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
This style is defined for the primary key fields of the database table. This style is set to each data record of the database table. But it is constrained to specific columns by specifying the fieldname of the data records. It is finally inserted in to the field style of each data record.
Key steps to set the style field are:
a) Define the fieldname of structure lvc_s_styl.
b) Define the style of structure lvc_s_styl.
c) Insert the structure lvc_s_styl in the fieldstyle field of each data record. Fieldstyle field is the internal table of type lvc_t_styl.
The selection of the style cl_gui_alv_grid=>mc_style_disabled blocks the fields of the respective column in editing mode. For example in the above table, the editing of column CONTACT_ID is deactivated.
DATA ls_style_record TYPE lvc_s_styl.
FIELD-SYMBOLS <ls_contact> TYPE ztt_db_table2.
LOOP AT lt_contact ASSIGNING <ls_contact>.
ls_style_record-fieldname = ‘CONTACT_ID’.
ls_style_record-style = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
INSERT ls_style_record INTO TABLE <ls_contact>-fieldstyle.
ENDLOOP.
The primary key fields of the table will now be available for insertion of new record. The insert button facilitates insertion of new record. The primary key fields of the ALV List for already existing data records are disabled for editing.