Cell Color in ABAP ALV Grid Report

2
9393

Dear SAPLearners, in this tutorial we will learn how to do cell color in ABAP ALV Grid report.

Step-by-Step Procedure

  1. Create an ABAP Program in SE38 transaction.
  2. Add an field called “CELLCOLOR” of type LVC_T_SCOL(table type) in your final internal table.
  3. Populate the field CELLCOLOR based on your business requirement.
  4. Assign CELLCOLOR to COLTAB_FIELDNAME of ALV Grid Layout structure.
  5. Create an ALV report using the function module REUSE_ALV_GRID_DISPLAY.

Business Scenario

In this demo example, we want to color the cell when quantity of purchase order is less than the 5.00.

ALV ABAP Source Code

*-------------------------------------------------------------*
*                 www.saplearners.com                         *
*-------------------------------------------------------------*
* Cell Color in ABAP ALV Report                               *
*-------------------------------------------------------------*

*--- Table declaration
TABLES: ekko.


*-- Type pool declaration
TYPE-POOLS: slis.


*--- Selection screen
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.


*--- Type declaration
TYPES: BEGIN OF lty_ekpo,
       ebeln  TYPE char30,  " Document no.
       ebelp  TYPE ebelp,   " Item no
       matnr  TYPE matnr,   " Material no
       werks  TYPE werks_d, " Plant
       ntgew  TYPE entge,   " Net weight
       gewei  TYPE egewe,   " Unit of weight
       cellcolor TYPE lvc_t_scol, "Cell Color
       END OF lty_ekpo.


*--- Internal table declaration
DATA: lt_ekpo            TYPE STANDARD TABLE OF lty_ekpo,
      lt_fieldcat        TYPE slis_t_fieldcat_alv.


*--- Work area declaration
DATA: wa_ekpo            TYPE lty_ekpo,
      wa_layout          TYPE slis_layout_alv,
      wa_cellcolor       TYPE lvc_s_scol,
      lv_index           TYPE sy-tabix.


*--- Start-of-selection event
START-OF-SELECTION.

* Select data from ekpo
  SELECT ebeln " Doc no
         ebelp " Item
         matnr " Material
         werks " Plant
         ntgew " Quantity
         gewei " Unit
         FROM ekpo
         INTO CORRESPONDING FIELDS OF TABLE lt_ekpo
         WHERE ebeln IN s_ebeln
         AND ntgew NE '0.00'.
  IF sy-subrc = 0.
    SORT lt_ekpo BY ebeln ebelp matnr .
  ENDIF.

*--- Set Cell colors
*    COL = 1 to 7,
*    INT = 1 = Intensified on, 0 = Intensified off
*    INV = 1 = Text Color, 0 = Background Color

* Color the Quantity cell where quantity is less than 5
   LOOP AT lt_ekpo INTO wa_ekpo.
    lv_index = sy-tabix.
  IF wa_ekpo-ntgew < 5.
    wa_cellcolor-fname = 'NTGEW'.
    wa_cellcolor-color-col = 6.
    wa_cellcolor-color-int = '1'.
    wa_cellcolor-color-inv = '0'.
    APPEND wa_cellcolor TO wa_ekpo-cellcolor.
    CLEAR: wa_cellcolor.
    MODIFY lt_ekpo FROM wa_ekpo INDEX lv_index TRANSPORTING cellcolor.
      ENDIF.
   ENDLOOP.

*--- Field Catalog
  PERFORM f_field_catalog.

*--- Layout
  PERFORM f_build_layout.


END-OF-SELECTION.

* Perform to display ALV report
  PERFORM f_alv_report_display.


*&---------------------------------------------------------------------*
*&      Form  sub_field_catalog
*&---------------------------------------------------------------------*
*       Build Field Catalog
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM f_field_catalog .

  DATA: lwa_fcat TYPE slis_fieldcat_alv.

*  Build Field Catalog
  lwa_fcat-col_pos        =  1.           "Column
  lwa_fcat-fieldname      =  'EBELN'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Doc. No'.    "Field Text
  APPEND lwa_fcat TO lt_fieldcat.

  lwa_fcat-col_pos        =  2.           "Column
  lwa_fcat-fieldname      =  'EBELP'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Item No'.   "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.


  lwa_fcat-col_pos        =  3.           "Column
  lwa_fcat-fieldname      =  'WERKS'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Plant'.     "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.

  lwa_fcat-col_pos        =  4.           "Column
  lwa_fcat-fieldname      =  'MATNR'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Material'.  "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.

  lwa_fcat-col_pos        =  5.           "Column
  lwa_fcat-fieldname      =  'NTGEW'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Quantity'.  "Field Text
  lwa_fcat-do_sum         = 'X'.          "Sum
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.

  lwa_fcat-col_pos        =  6.           "Column
  lwa_fcat-fieldname      =  'GEWEI'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'UOM'.       "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.
  ENDFORM.                    " sub_field_catalog

*&---------------------------------------------------------------------*
*&      Form  f_populate_layout
*&---------------------------------------------------------------------*
*       Populate ALV layout
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM f_build_layout.

  CLEAR wa_layout.
  wa_layout-colwidth_optimize = 'X'.         " Optimization of Col width
  wa_layout-coltab_fieldname = 'CELLCOLOR'.  " Cell color Column Name

ENDFORM.                    " f_populate_layout
*&---------------------------------------------------------------------*
*&      Form  f_alv_report_display
*&---------------------------------------------------------------------*
FORM f_alv_report_display .

* ALV report
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      is_layout                = wa_layout
      it_fieldcat              = lt_fieldcat
    TABLES
      t_outtab                 = lt_ekpo
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
  ENDIF.

ENDFORM.                    " f_alv_report_display

ALV Output

Cell Color in ABAP ALV Grid

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!

Other ALV ABAP Tutorials

How to add Sub-total text in ABAP ALV Grid

2 COMMENTS

  1. Hey.
    If you want to handle ANY /! formatting of a particular cell in ALV, here s the way.

    form update_style using
    x type lvc_fname ” x is ‘MATNR’, ‘VBELN’, ‘BELNR’…. whatever in your fieldcatalog
    y type int4 ” y = 1,2,3…..
    update_mask type xstring
    update_style type xstring.

    READ table gt_alv into gs_alv index y.
    tabstyle = gs_alv-tabstyle.
    READ table tabstyle into s_style with key fieldname = x.

    ” check if fieldname already exists in gs_alv-tabstyle (insert or update)

    s_style-style = s_style-style BIT-AND update_mask.
    s_style-style = s_style-style BIT-OR update_style.

    ” Update or insert gs_alv-tabstyle from s_style
    ” update gt_alv from gs_alv

    ENDFORM.

    How to assign a color to a row in ALV
    (But you cant have Cell background and Font Color at the same time !)

    perform update_style USING x y ‘FFFFFFE0’ update_style.

    update_style can be :

    ‘00000000’. ” Disable any color
    ‘00000011’. ” White Font
    ‘00000017’. ” Red fond
    ‘00000013’. ” gray font
    ‘00000014’. ” Yellow font
    ‘00000015’. ” blue font
    ‘00000016’. ” green font
    ‘00000018’. ” orange font
    ‘00000011’. ” black font

    ‘0000000D’. ” background dark blue
    ‘0000000A’. ” background fluo blue
    ‘00000005’. ” background blue
    ‘00000003’. ” background light blue
    ‘00000000’. ” background gray
    ‘00000001’. ” background light gray
    ‘00000004’. ” backgorund yellow
    ‘0000000C’. ” background dark yellow
    ‘00000006’. ” background green
    ‘0000000E’. ” background dark green
    ‘00000010’. ” background dark orange
    ‘0000000F’. ” background red
    ‘00000007’. ” background pink
    ‘00000008’. ” background orange
    ‘00000001’. ” background standard

    If you want to handle editable, bold, italic, underline… properties

    perform update_style USING x y ‘FFFFFF9F’ ‘00000020’. ” set bold
    perform update_style USING x y ‘FFFFFF9F’ ‘00000040’. ” unset bold property

    perform update_style USING x y ‘FFFFF9FF’ ‘00000200’. ” Underline
    perform update_style USING x y ‘FFFFF9FF’ ‘00000400’. ” stop underlying

    perform update_style USING x y ‘FFFFF57F’ ‘00000080’ ” italic

    perform update_style USING x y ‘FF9FFFFF’ ‘00200000’ ” hotspot
    perform update_style USING x y ‘FF9FFFFF’ ‘00400000’ ” no hotspot

    perform update_style USING x y ‘F9E7FFFF’ ‘00080000’ ” Editable
    perform update_style USING x y ‘F9E7FFFF’ ‘00100000’ ” Non Editable

    For alignments, update_mask is ‘DFFFF57F’
    And update_style can be :

    DATA alv_style_align_left_top(4) TYPE x VALUE ‘00000800’.
    DATA alv_style_align_center_top(4) TYPE x VALUE ‘00001000’.
    DATA alv_style_align_right_top(4) TYPE x VALUE ‘00001800’.
    DATA alv_style_align_left_center(4) TYPE x VALUE ‘00002000’.
    DATA alv_style_align_center_center(4) TYPE x VALUE ‘00002800’.
    DATA alv_style_align_right_center(4) TYPE x VALUE ‘00003000’.
    DATA alv_style_align_left_bottom(4) TYPE x VALUE ‘00003800’.
    DATA alv_style_align_center_bottom(4) TYPE x VALUE ‘00004000’.
    DATA alv_style_align_right_bottom(4) TYPE x VALUE ‘00004800’.

    If you want to handle borders, you have to modify s_style-style2 with :

    ‘FFFBFFFF’ ‘00040000’ ” Remove top border
    ‘FFF7FFFF’ ‘00080000’ ” Remove bottom border
    ‘FFFEFFFF’ ‘00010000’ ” Remove left border
    ‘FFFDFFFF’ ‘00020000’ ” Remove right border

    And after all of this (even after set_table_for_first_display)
    you just need to call :
    I
    dont use FORMs but METHODS because i wrote an implentation of class
    ZCL_GUI_ALV_GRID but Method REFRESH could be a FORM in your programs.

    METHOD REFRESH.
    DATA ls_stable TYPE lvc_s_stbl.
    ls_stable-row = ‘X’.
    ls_stable-col = ‘X’.

    me->set_frontend_layout( is_layout = me->is_layout ).

    CALL METHOD me->refresh_table_display
    EXPORTING
    is_stable = ls_stable
    * i_soft_refresh = ‘X’ ”
    EXCEPTIONS
    finished = 1
    OTHERS = 2.
    ret = me.
    ENDMETHOD.

    For me, x and y are not mandatory fields so :
    You can loop over all lines if you want to set a property to a column (y is unset)

    ASSIGN mt_outtab->* TO .
    CHECK IS ASSIGNED.
    LOOP AT ASSIGNING .
    update_style( x sy-tabix update_mask update_style )

    or loop over your catalog when you want to set a property to a line (x is unset)

    LOOP at me->get_frontend_fieldcatalog( ) in ls_fcat
    update_style( ls_fcat-fieldname y update_mask update_style )

    For more possibilities, check link
    https://stackoverflow.com/questions/27000012/abap-alv-grid-merge-cells-and-style-formating-of-cells

    Simon

    • Hi Simon,

      Thanks for your code snippet.
      Can we use it and create a new blog on our site. It would be good to have this code as post instead of a comment.
      If you would like to share these kind of code snippets with us, we are open to welcome you as a contributor.
      Please reach out to us saplearners9@gmail.com

      Thanks,
      Akash

LEAVE A REPLY

Please enter your comment!
Please enter your name here