How to color the cell in SAP ALV Display?

The old ALV Display model created using CL_GUI_ALV_GRID enables the user to colour particular cells of ALV Display. Every colour is uniquely identified through a colour code. The following table illustrates the different colour codes.

Type of colourCOLINTINV
Orange711
Green510
Red610
Yellow310
Blue110
Fig. Color codes for cells of ALV Display

The above fields COL,INT,INV are the elements of [lvc_s_scol – COLOR].

There are three key steps involved in colouring a particular cell / column of a SAP ALV Display.

  1. Extend the Internal table with the column lvc_t_scol.
  2. Assignment of column ‘lvc_t_scol’ to the field ‘CTAB_FNAME’ of layout.
  3. Assign the colour codes to the cells of SAP ALV Display.
  • Extension of Internal table.

The internal table which contains the data of the ALV Display is extended with the additional column ‘lvc_t_scol’. The table lvc_t_scol holds the details of the colour code. Let us look at the following structure extension.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

              TYPES: cell_with_color TYPE lvc_t_scol,

            END OF ty_contact.

DATA lt_contact TYPE TABLE OF ty_contact.

  • ASSIGNMENT of field ‘CTAB_FNAME’ to the layout.

The ALV Table recognizes the newly added field of type ‘LVC_T_SCOL’ through the field ‘CTAB_FNAME’ of the layout structure.

DATA ls_layout TYPE lvc_s_layo.

ls_layout-ctab_fname = ‘CELL_WITH_COLOR’.

the above value must be specified in capital letters. otherwise the ALV Grid cannot recognize the newly added field.

  • Assignment of Colour code to the cells of ALV Display.

The final step is to assign the colour codes to the cells of the ALV Table Display. The column is mentioned through the field ‘FNAME’ of the structure ‘lvc_s_scol’.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

DATA ls_cell_color TYPE lvc_s_scol.

LOOP AT lt_contact ASSIGNING <ls_contact> WHERE age > 30.

              ls_cell_color-fname = ‘CONTACT_NAME’.

              ls_cell_color-col = 7.

              ls_cell_color-int= 1.

              ls_cell_color-inv = 1.

              APPEND ls_cell_color TO <ls_contact>-cell_with_color.

“The internal table is filled with color code details.

ENDLOOP.

              The above code snippet sets the colour of the ALV Cells. It is constrained to the contacts whose age are more than 30. The column specified here to be colored is ‘CONTACT_NAME’.

How to display color for each row in SAP ALV List?

The old SAP ALV Display facilitates the user to color the cells, lines of record, columns etc. Every color is uniquely identified through a colour code. The following table illustrates the different colour codes.

Type of ColorCode
BlueC110
YellowC310
OrangeC711
GreenC510
RedC610
fig. Table of Colour Codes.

There are three important steps involved in colouring a line of record.

  1. Extend the internal table with the field (colour code) of Type C
  2. Set the value of field ‘info_fname’ of the layout.
  3. Assign the colour code to each line of record.
  • Extension of Internal table.

In order to colour the complete line of record, a new field must be added. This field is of Data type c. The field holds the colour code of the particular line of record.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

TYPES: line_of_record (4) TYPE c,

              END OF ty_contact.

DATA lt_contact TYPE TABLE OF ty_contact.

  • ASSIGNMENT of field INFO_FNAME in the layout.

The ALV List can be able to identify the newly added field (like above) through the field ‘INFO_FNAME’ of layout.

The Assignment is as shown below.

DATA ls_layout TYPE lvc_s_layo.

ls_layout-info_fname = ‘LINE_OF_RECORD’.

Please remember that the value assigned must be in capital letters. Otherwise it will not be able to identify correctly.

  • ASSIGNMENT OF COLOUR CODE TO EACH LINE OF RECORD.

The last and final step involved is to colour each line of record with the unique colour code. Each line of record is read through ‘LOOP AT’ loop. The Rows can be filtered through additional constraints. Let us look at the following example.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

LOOP AT lt_contact ASSIGNING <ls_contact> WHERE contact_age > 30.

<ls_contact> – line_of_record = ‘C310’.

ENDLOOP.

              As shown in above code, each line of records in SAP ALV List is set with the colour code ‘C310’. This colour code indicates the colour ‘YELLOW’.