Skip to Content
avatar image
Former Member

change color in alv column

how do change color of a column in ALV.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • avatar image
    Former Member
    Jul 31, 2008 at 05:26 AM

    hi,

    Refer to the link.

    http://www.sapdev.co.uk/sap-reporting/sap-alv/alvgrid_cellcolor.htm

    Regards

    Sumit Agarwal

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 31, 2008 at 05:28 AM

    Hi Sandeep,

    Check these threads:

    Coloring Colums in ALV:

    Coloring cells in alv

    Coloring Rows in ALV:

    Coloring Rows in ALV

    Regards,

    Chandra Sekhar

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 31, 2008 at 05:30 AM

    Hi

    Set the attribute EMPHASIZE for that field in the FIELD CATALOG.

    Hope it helps.

    Murthy

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 31, 2008 at 05:30 AM
    Add comment
    10|10000 characters needed characters exceeded

  • Jul 31, 2008 at 05:30 AM

    Hi

    The below abap program shows how to change the colour of individual ALV cells /fields. Only a small number of changes are required from a basic ALV grid which include adding a new field to ALV data declaration table(it_ekko), populating this field with the field name identifier and colour attributes and finally adding an entry to layout control work area. These changes are highlighted in bold below.

    REPORT zdemo_alvgrid .

    REPORT ZALV_CELLCOLOR.

    TABLES: ekko.

    type-pools: slis. "ALV Declarations

    *Data Declaration

    *----


    TYPES: BEGIN OF t_ekko,

    ebeln TYPE ekpo-ebeln,

    ebelp TYPE ekpo-ebelp,

    statu TYPE ekpo-statu,

    aedat TYPE ekpo-aedat,

    matnr TYPE ekpo-matnr,

    menge TYPE ekpo-menge,

    meins TYPE ekpo-meins,

    netpr TYPE ekpo-netpr,

    peinh TYPE ekpo-peinh,

    CELLCOLOR TYPE LVC_T_SCOL,

    END OF t_ekko.

    DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,

    wa_ekko TYPE t_ekko.

    *ALV data declarations

    data: fieldcatalog type slis_t_fieldcat_alv with header line,

    gd_tab_group type slis_t_sp_group_alv,

    gd_layout type slis_layout_alv,

    gd_repid like sy-repid,

    gt_events type slis_t_event,

    gd_prntparams type slis_print_alv.

    ************************************************************************

    *Start-of-selection.

    START-OF-SELECTION.

    perform data_retrieval.

    perform build_fieldcatalog.

    perform build_layout.

    perform set_cell_colours.

    perform display_alv_report.

    &----


    *& Form BUILD_FIELDCATALOG

    &----


    • Build Fieldcatalog for ALV Report

    ----


    form build_fieldcatalog.

    • There are a number of ways to create a fieldcat.

    • For the purpose of this example i will build the fieldcatalog manualy

    • by populating the internal table fields individually and then

    • appending the rows. This method can be the most time consuming but can

    • also allow you more control of the final product.

    • Beware though, you need to ensure that all fields required are

    • populated. When using some of functionality available via ALV, such as

    • total. You may need to provide more information than if you were

    • simply displaying the result

    • I.e. Field type may be required in-order for

    • the 'TOTAL' function to work.

    fieldcatalog-fieldname = 'EBELN'.

    fieldcatalog-seltext_m = 'Purchase Order'.

    fieldcatalog-col_pos = 0.

    fieldcatalog-outputlen = 10.

    fieldcatalog-emphasize = 'X'.

    fieldcatalog-key = 'X'.

    • fieldcatalog-do_sum = 'X'.

    • fieldcatalog-no_zero = 'X'.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'EBELP'.

    fieldcatalog-seltext_m = 'PO Item'.

    fieldcatalog-col_pos = 1.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'STATU'.

    fieldcatalog-seltext_m = 'Status'.

    fieldcatalog-col_pos = 2.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'AEDAT'.

    fieldcatalog-seltext_m = 'Item change date'.

    fieldcatalog-col_pos = 3.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'MATNR'.

    fieldcatalog-seltext_m = 'Material Number'.

    fieldcatalog-col_pos = 4.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'MENGE'.

    fieldcatalog-seltext_m = 'PO quantity'.

    fieldcatalog-col_pos = 5.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'MEINS'.

    fieldcatalog-seltext_m = 'Order Unit'.

    fieldcatalog-col_pos = 6.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'NETPR'.

    fieldcatalog-seltext_m = 'Net Price'.

    fieldcatalog-col_pos = 7.

    fieldcatalog-outputlen = 15.

    fieldcatalog-do_sum = 'X'.

    fieldcatalog-datatype = 'CURR'.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    fieldcatalog-fieldname = 'PEINH'.

    fieldcatalog-seltext_m = 'Price Unit'.

    fieldcatalog-col_pos = 8.

    append fieldcatalog to fieldcatalog.

    clear fieldcatalog.

    endform. " BUILD_FIELDCATALOG

    &----


    *& Form BUILD_LAYOUT

    &----


    • Build layout for ALV grid report

    ----


    form build_layout.

    gd_layout-no_input = 'X'.

    gd_layout-colwidth_optimize = 'X'.

    gd_layout-totals_text = 'Totals'(201).

    gd_LAYOUT-coltab_fieldname = 'CELLCOLOR'. "CTAB_FNAME

    endform. " BUILD_LAYOUT

    &----


    *& Form DISPLAY_ALV_REPORT

    &----


    • Display report using ALV grid

    ----


    form display_alv_report.

    gd_repid = sy-repid.

    call function 'REUSE_ALV_GRID_DISPLAY'

    exporting

    i_callback_program = gd_repid

    i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM

    • i_callback_user_command = 'USER_COMMAND'

    • i_grid_title = outtext

    is_layout = gd_layout

    it_fieldcat = fieldcatalog[]

    • it_special_groups = gd_tabgroup

    • it_events = gt_events

    • is_print = gd_prntparams

    i_save = 'X'

    • is_variant = z_template

    tables

    t_outtab = it_ekko

    exceptions

    program_error = 1

    others = 2.

    if sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    endif.

    endform. " DISPLAY_ALV_REPORT

    &----


    *& Form DATA_RETRIEVAL

    &----


    • Retrieve data form EKPO table and populate itab it_ekko

    ----


    form data_retrieval.

    select ebeln ebelp statu aedat matnr menge meins netpr peinh

    up to 10 rows

    from ekpo

    into CORRESPONDING FIELDS OF TABLE it_ekko.

    endform. " DATA_RETRIEVAL

    ----


    • Form TOP-OF-PAGE *

    ----


    • ALV Report Header *

    ----


    Form top-of-page.

    *ALV Header declarations

    data: t_header type slis_t_listheader,

    wa_header type slis_listheader,

    t_line like wa_header-info,

    ld_lines type i,

    ld_linesc(10) type c.

    • Title

    wa_header-typ = 'H'.

    wa_header-info = 'EKKO Table Report'.

    append wa_header to t_header.

    clear wa_header.

    • Date

    wa_header-typ = 'S'.

    wa_header-key = 'Date: '.

    CONCATENATE sy-datum+6(2) '.'

    sy-datum+4(2) '.'

    sy-datum(4) INTO wa_header-info. "todays date

    append wa_header to t_header.

    clear: wa_header.

    • Total No. of Records Selected

    describe table it_ekko lines ld_lines.

    ld_linesc = ld_lines.

    concatenate 'Total No. of Records Selected: ' ld_linesc

    into t_line separated by space.

    wa_header-typ = 'A'.

    wa_header-info = t_line.

    append wa_header to t_header.

    clear: wa_header, t_line.

    call function 'REUSE_ALV_COMMENTARY_WRITE'

    EXPORTING

    it_list_commentary = t_header.

    • i_logo = 'Z_LOGO'.

    endform. "top-of-page

    &----


    *& Form SET_CELL_COLOURS

    &----


    • Set colour of individual ALV cell, field

    ----


    FORM SET_CELL_COLOURS .

    DATA: WA_CELLCOLOR TYPE LVC_S_SCOL.

    DATA: ld_index TYPE SY-TABIX.

    LOOP AT IT_EKKO into wa_ekko.

    LD_INDEX = SY-TABIX.

    • Set colour of EBELN field to various colors based on sy-tabix value

    WA_CELLCOLOR-FNAME = 'EBELN'.

    WA_CELLCOLOR-COLOR-COL = sy-tabix. "color code 1-7, if outside rage defaults to 7

    WA_CELLCOLOR-COLOR-INT = '1'. "1 = Intensified on, 0 = Intensified off

    WA_CELLCOLOR-COLOR-INV = '0'. "1 = text colour, 0 = background colour

    APPEND WA_CELLCOLOR TO wa_ekko-CELLCOLOR.

    MODIFY it_ekko from wa_ekko INDEX ld_index TRANSPORTING CELLCOLOR.

    • Set colour of NETPR field to color 4 if gt 0

    if wa_ekko-netpr gt 0.

    WA_CELLCOLOR-FNAME = 'NETPR'.

    WA_CELLCOLOR-COLOR-COL = 4. "color code 1-7, if outside rage defaults to 7

    WA_CELLCOLOR-COLOR-INT = '0'. "1 = Intensified on, 0 = Intensified off

    WA_CELLCOLOR-COLOR-INV = '0'. "1 = text colour, 0 = background colour

    APPEND WA_CELLCOLOR TO wa_ekko-CELLCOLOR.

    MODIFY it_ekko from wa_ekko INDEX ld_index TRANSPORTING CELLCOLOR.

    endif.

    • Set colour of AEDAT field text to red(6)

    WA_CELLCOLOR-FNAME = 'AEDAT'.

    WA_CELLCOLOR-COLOR-COL = 6. "color code 1-7, if outside rage defaults to 7

    WA_CELLCOLOR-COLOR-INT = '0'. "1 = Intensified on, 0 = Intensified off

    WA_CELLCOLOR-COLOR-INV = '1'. "1 = text colour, 0 = background colour

    APPEND WA_CELLCOLOR TO wa_ekko-CELLCOLOR.

    MODIFY it_ekko from wa_ekko INDEX ld_index TRANSPORTING CELLCOLOR.

    ENDLOOP.

    ENDFORM. " SET_CELL_COLOURS

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 31, 2008 at 05:33 AM

    Hi Sandeep,

    here is the code to color the cell... just go through this..

    TABLES: MARA , MAKT .

    DATA: LAYOUT TYPE LVC_S_LAYO .

    DATA: CONT1 TYPE REF TO CL_GUI_CUSTOM_CONTAINER .

    DATA: GRID1 TYPE REF TO CL_GUI_ALV_GRID .

    DATA: FCAT1 TYPE LVC_T_FCAT .

    DATA: WA_FCAT1 LIKE LINE OF FCAT1 .

    TYPES: BEGIN OF TYPE_ITAB1 ,

    MATNR LIKE MARA-MATNR ,

    MTART LIKE MARA-MTART,

    MATKL LIKE MARA-MATKL ,

    MAKTX LIKE MAKT-MAKTX ,

    COL TYPE lvc_t_scol,

    END OF TYPE_ITAB1 .

    DATA: IT_ITAB1 TYPE STANDARD TABLE OF TYPE_ITAB1 WITH HEADER LINE .

    DATA: LEN TYPE I.

    LAYOUT-INFO_FNAME = 'COL' .

    break devuser .

    WA_FCAT1-TABNAME = 'IT_ITAB1' .

    WA_FCAT1-FIELDNAME = 'MATNR' .

    WA_FCAT1-COLTEXT = 'Material' .

    WA_FCAT1-OUTPUTLEN = 18 .

    APPEND WA_FCAT1 TO FCAT1 .

    CLEAR WA_FCAT1 .

    WA_FCAT1-TABNAME = 'IT_ITAB1' .

    WA_FCAT1-FIELDNAME = 'MTART' .

    WA_FCAT1-COLTEXT = 'Material Type' .

    WA_FCAT1-OUTPUTLEN = 15 .

    APPEND WA_FCAT1 TO FCAT1 .

    CLEAR WA_FCAT1 .

    WA_FCAT1-TABNAME = 'IT_ITAB1' .

    WA_FCAT1-FIELDNAME = 'MATKL' .

    WA_FCAT1-COLTEXT = 'Material Group' .

    WA_FCAT1-OUTPUTLEN = 15 .

    APPEND WA_FCAT1 TO FCAT1 .

    CLEAR WA_FCAT1 .

    WA_FCAT1-TABNAME = 'IT_ITAB1' .

    WA_FCAT1-FIELDNAME = 'MAKTX' .

    WA_FCAT1-COLTEXT = 'Description' .

    WA_FCAT1-OUTPUTLEN = 25 .

    APPEND WA_FCAT1 TO FCAT1 .

    CLEAR WA_FCAT1 .

    SELECT AMATNR AMTART AMATKL BMAKTX INTO TABLE IT_ITAB1 FROM MARA AS A INNER JOIN MAKT AS B ON AMATNR = BMATNR .

    DESCRIBE TABLE IT_ITAB1 LINES LEN .

    LEN = LEN .

    *IT_ITAB1-COL = 'C601' .

    MODIFY IT_ITAB1 INDEX LEN FROM IT_ITAB1

    TRANSPORTING COL .

    CALL SCREEN 100 .

    &----


    *& Module SHOWALV OUTPUT

    &----


    • text

    ----


    module SHOWALV output.

    CREATE OBJECT cont1

    EXPORTING

    • PARENT =

    container_name = 'CUSTOM'

    • STYLE =

    • LIFETIME = lifetime_default

    • REPID =

    • DYNNR =

    • NO_AUTODEF_PROGID_DYNNR =

    • EXCEPTIONS

    • CNTL_ERROR = 1

    • CNTL_SYSTEM_ERROR = 2

    • CREATE_ERROR = 3

    • LIFETIME_ERROR = 4

    • LIFETIME_DYNPRO_DYNPRO_LINK = 5

    • others = 6

    .

    IF sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    ENDIF.

    CREATE OBJECT grid1

    EXPORTING

    • I_SHELLSTYLE = 0

    • I_LIFETIME =

    i_parent = CONT1

    • I_APPL_EVENTS = space

    • I_PARENTDBG =

    • I_APPLOGPARENT =

    • I_GRAPHICSPARENT =

    • I_NAME =

    • I_FCAT_COMPLETE = SPACE

    • EXCEPTIONS

    • ERROR_CNTL_CREATE = 1

    • ERROR_CNTL_INIT = 2

    • ERROR_CNTL_LINK = 3

    • ERROR_DP_CREATE = 4

    • others = 5

    .

    IF sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    ENDIF.

    CALL METHOD grid1->set_table_for_first_display

    EXPORTING

    • I_BUFFER_ACTIVE =

    • I_BYPASSING_BUFFER =

    • I_CONSISTENCY_CHECK =

    • I_STRUCTURE_NAME =

    • IS_VARIANT =

    • I_SAVE =

    • I_DEFAULT = 'X'

    IS_LAYOUT = LAYOUT

    • IS_PRINT =

    • IT_SPECIAL_GROUPS =

    • IT_TOOLBAR_EXCLUDING =

    • IT_HYPERLINK =

    • IT_ALV_GRAPHICS =

    • IT_EXCEPT_QINFO =

    • IR_SALV_ADAPTER =

    CHANGING

    it_outtab = IT_ITAB1[]

    IT_FIELDCATALOG = FCAT1

    • IT_SORT =

    • IT_FILTER =

    • EXCEPTIONS

    • INVALID_PARAMETER_COMBINATION = 1

    • PROGRAM_ERROR = 2

    • TOO_MANY_LINES = 3

    • others = 4

    .

    IF sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    ENDIF.

    endmodule. " SHOWALV OUTPUT

    Thanks & Regards

    Ashu Singh

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 31, 2008 at 05:39 AM

    Hi Sandeep.

    This question has been asked Lot of times.

    I would like to suggest,

    Search in SDN, SDN weblogs and SDN wiki.

    I would like to suggest a couple of references,

    [SDN Code Gallery - Standard Reference for Coloring a Row and Column in ALV (OOPS)|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/coloring%2ba%2brow%2band%2bcolumn%2bin%2balv%2b(OOPS)]

    [SDN Code Gallery - Standard Reference for Change color of individual ALV rows|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/change%2bcolor%2bof%2bindividual%2balv%2brows]

    [SDN Code Gallery - Standard Reference for ABAP - AVL Grid Displaying in Colour|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/abap%2b-%2bavl%2bgrid%2bdisplaying%2b%2bin%2bcolour]

    Hope that's usefull.

    Good Luck & Regards.

    Harsh Dave

    Add comment
    10|10000 characters needed characters exceeded