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’.