Skip to Content
avatar image
Former Member

OO ALV colour cells

Hello,

I have implemented a code in which i wanto to colour some cells in the ALV according to a condition.

My code is.

LOOP AT GT_ALVTABLE INTO GW_ALVTABLE.
        IF GW_ALVTABLE-APLFZ <> '0'.
          LV_S_COLOR-FNAME = 'NAME1'.
          LV_S_COLOR-COLOR-COL =  6. "red
          LV_S_COLOR-COLOR-INT =  1.
          LV_S_COLOR-COLOR-INV =  0.
          APPEND LV_S_COLOR TO LV_T_COLOR.
          GW_ALVTABLE-T_COLOR = LV_T_COLOR.
          MODIFY GT_ALVTABLE FROM GW_ALVTABLE.
        ENDIF.

      ENDLOOP.

      TRY.
          GCL_COLUMNS->SET_COLOR_COLUMN( 'T_COLOR' ).
        CATCH CX_SALV_DATA_ERROR.
      ENDTRY.

    GCL_TABLE->DISPLAY( ).

T_COLOR is a column in the GT_ALVTABLE

TYPE LVC_T_SCOL

When i execute it it doesnt colour those cells.

Have i forget something or it cant be done by this way ?

Thx in advance.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

10 Answers

  • Best Answer
    avatar image
    Former Member
    Jul 30, 2009 at 08:53 AM

    hi,

    You can refer to any of the following link:

    ALV Color a CELL?

    alv color rows

    ALV color codes

    Thank You.

    Regards,

    Dhanalakshmi L

    Add comment
    10|10000 characters needed characters exceeded

    • *continuing the code

      LOOP AT itab assigning  <f_wA_ITAB>.
          REFRESH : color_tab[].
      
      
          CASE sy-tabix.
            WHEN 1.
              wa_color-fname = 'MATNR'.
              wa_color-color-col = 6.  " red color
              APPEND wa_color TO color_tab.
      
              wa_color-fname = 'MAKTX'.
              wa_color-color-col = 5. " green color
              APPEND wa_color TO color_tab.
      
            WHEN OTHERS.
      
              wa_color-fname = 'MATNR'.
              wa_color-color-col = 3. "yellow
              APPEND wa_color TO color_tab.
      
              wa_color-fname = 'MAKTX'.
              wa_color-color-col = 7. "violet
              APPEND wa_color TO color_tab.
      
      
          ENDCASE.
          INSERT LINES OF color_tab INTO TABLE <f_wA_ITAB>-colortab.
        ENDLOOP.

      * pass the column name of the internal table
      * which contains the color sequence to the layout of the grid
        wa_layo-ctab_fname = 'COLORTAB'.
      
      * display the grid
      
        CALL METHOD gc_grid->set_table_for_first_display
          EXPORTING
            is_layout                     = wa_layo
          CHANGING
            it_outtab                     = itab[]
            it_fieldcatalog               = it_fcat[]
          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.

      I hope you understand the code and teh concept.

      Regards,

      Ankur Parab

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

  • Jul 30, 2009 at 07:37 AM

    Hi,

    Have you passed the name 'T_COLOR' of the column in the field CTAB_FNAME of the Layout of the ALV grid object?

    The field CTAB_FNAME of the Layout of the ALV grid object should contain the column name of your internal table which hold the complex color coding table.

    Regards,

    Ankur Parab

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 30, 2009 at 07:38 AM
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 30, 2009 at 07:43 AM

    Hi,

    Add a field in your internal table(itab) used for displaying data, let's say cellcolors of type lvc_t_scol.

    types: begin of x_flight.

    include structure sflight.

    types: cellcolors TYPE lvc_t_scol,

    end of x_flight.

    set field ctab_fname in layout parameter

    layout-ctab_fname = 'CELLCOLORS'.

    while populating your output table, specify color code in CELLCOLORS

    loop at itab into wa.

    if wa-field1 = 'X'.

    wa_col-fname = 'PRICE'. "Cell to be coloured

    wa_col-color-col = '6'. "Color

    wa_col-color-int = '0'. "intensified

    append wa_col to wa-cellcolors.

    endif.

    wa-field2 = 'ABC'.

    append wa to itab.

    endloop.

    just pass the layout and the internal table in OOPS method SET_TABLE_FoR_FIRST_DISPLAY

    Thanks & regards,

    ShreeMohan

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 30, 2009 at 07:51 AM

    HI,

    I think after LOOP ... ENDLOOP, you are setting the color. Before setting the color, you have to call that column and set the color details. The below code may help you ..

    -


    First Part----


    LOOP AT GT_ALVTABLE INTO GW_ALVTABLE.

    IF GW_ALVTABLE-APLFZ '0'.

    LV_S_COLOR-FNAME = 'NAME1'.

    LV_S_COLOR-COLOR-COL = 6. "red

    LV_S_COLOR-COLOR-INT = 1.

    LV_S_COLOR-COLOR-INV = 0.

    APPEND LV_S_COLOR TO LV_T_COLOR.

    GW_ALVTABLE-T_COLOR = LV_T_COLOR.

    MODIFY GT_ALVTABLE FROM GW_ALVTABLE.

    ENDIF.

    ENDLOOP.

    __________________Second Part______________________

    TRY .

    gr_p_column ?= gr_p_columns->get_column( 'NAME1 ).

    ls_color-col = 6.

    ls_color-int = 0.

    ls_color-inv = 0.

    CATCH cx_salv_not_found INTO lv_oref.

    lv_text = lv_oref->get_text( ).

    CLEANUP.

    ENDTRY.

    gr_p_column->set_color( ls_color ).

    _________________Third Part____________________________

    TRY.

    GCL_COLUMNS->SET_COLOR_COLUMN( 'T_COLOR' ).

    CATCH CX_SALV_DATA_ERROR.

    ENDTRY.

    GCL_TABLE->DISPLAY( ).

    _________________________________________________________

    The 2nd section code I have pasted from my own code. Just refer this section to match yours(declaration of ref objects).

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 30, 2009 at 07:55 AM

    Hi check this below program it will help you.

    [http://help-abap.blogspot.com/search/label/SALV%20Tutorial]

    it uses cl_salv_table.

    Regards

    Sunil

    Edited by: sunil kumar on Jul 30, 2009 1:50 PM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 30, 2009 at 08:13 AM
    Add comment
    10|10000 characters needed characters exceeded

  • Jul 30, 2009 at 08:36 AM

    link:[https://wiki.sdn.sap.com/wiki/display/Snippets/ColoringaRowandColumninALV+%28OOPS%29]

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 30, 2009 at 10:49 AM

    hi,

    When you loop at GT_ALVTABLE if you can just maintain an index and when you finally Modify the internal table

    use that index to color the cells.

    Eg:

    MODIFY GT_ALVTABLE INDEX l_index FROM GW_ALVTABLE

    with this even if it does not work you will be able to tell whether

    it is reaching till the correct cell for colouring.

    Hope this works

    Add comment
    10|10000 characters needed characters exceeded