Skip to Content
avatar image
Former Member

Cell Color in ALV Object Model

Hi All,

Is it possible to color individual cells in Simple Two-Dimensional ALV List using ALV Object Model? If yes, please help me get started on the same.

Thanks and Regards,

Vidya.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

6 Answers

  • Best Answer
    Sep 02, 2008 at 11:22 AM

    Hi,

    Yes you can.

    have a look at demo program SALV_DEMO_TABLE_COLUMNS.

    It colors a column but if you fill parameter FNAME then just a cell is filled with a color.

    Regards,

    Bert

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 02, 2008 at 11:17 AM

    hai ,

    we can give individual color to the each cells .

    example

    REPORT ZALV_LIST1.

    TABLES:

    SPFLI.

    TYPE-POOLS:

    SLIS.

    PARAMETERS:

    P_COL TYPE I ,

    P_ROW TYPE I,

    P_COLOR(4) TYPE C .

    DATA:

    T_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,

    FS_FIELDCAT LIKE LINE OF T_FIELDCAT,

    FS_LAYOUT TYPE SLIS_LAYOUT_ALV ,

    W_COLOR(4) ,

    W_ROW TYPE I,

    W_FIELDNAME(20),

    W_PROG TYPE SY-REPID.

    DATA:

    BEGIN OF T_SPFLI OCCURS 0,

    COLOR(4),

    CHECKBOX ,

    CELL TYPE SLIS_T_SPECIALCOL_ALV,

    CARRID TYPE SPFLI-CARRID,

    CONNID TYPE SPFLI-CONNID,

    CITYFROM TYPE SPFLI-CITYFROM,

    CITYTO TYPE SPFLI-CITYTO,

    DISTANCE TYPE SPFLI-DISTANCE,

    END OF T_SPFLI.

    DATA:

    FS_CELL LIKE LINE OF T_SPFLI-CELL.

    SELECT *

    FROM SPFLI

    INTO CORRESPONDING FIELDS OF TABLE T_SPFLI.

    W_COLOR = P_COLOR.

    T_SPFLI-COLOR = P_COLOR.

    IF P_COL IS INITIAL AND P_ROW GT 0.

    MODIFY T_SPFLI INDEX P_ROW TRANSPORTING COLOR.

    ENDIF.

    FS_FIELDCAT-FIELDNAME = 'CARRID'.

    FS_FIELDCAT-REF_TABNAME = 'SPFLI'.

    FS_FIELDCAT-COL_POS = 1.

    FS_FIELDCAT-KEY = 'X'.

    FS_FIELDCAT-HOTSPOT = 'X'.

    APPEND FS_FIELDCAT TO T_FIELDCAT.

    CLEAR FS_FIELDCAT .

    FS_FIELDCAT-FIELDNAME = 'CONNID'.

    FS_FIELDCAT-REF_TABNAME = 'SPFLI'.

    FS_FIELDCAT-COL_POS = 2.

    FS_FIELDCAT-KEY = 'X'.

    FS_FIELDCAT-HOTSPOT = 'X'.

    APPEND FS_FIELDCAT TO T_FIELDCAT.

    CLEAR FS_FIELDCAT .

    FS_FIELDCAT-FIELDNAME = 'DISTANCE'.

    FS_FIELDCAT-REF_TABNAME = 'SPFLI'.

    FS_FIELDCAT-COL_POS = 3.

    FS_FIELDCAT-KEY = ' '.

    FS_FIELDCAT-EDIT = 'X'.

    APPEND FS_FIELDCAT TO T_FIELDCAT.

    CLEAR FS_FIELDCAT.

    FS_FIELDCAT-FIELDNAME = 'CITYFROM'.

    FS_FIELDCAT-REF_TABNAME = 'SPFLI'.

    FS_FIELDCAT-COL_POS = 4.

    FS_FIELDCAT-KEY = ' '.

    APPEND FS_FIELDCAT TO T_FIELDCAT.

    LOOP AT T_FIELDCAT INTO FS_FIELDCAT.

    IF FS_FIELDCAT-COL_POS EQ P_COL.

    FS_FIELDCAT-EMPHASIZE = P_COLOR.

    W_FIELDNAME = FS_FIELDCAT-FIELDNAME.

    IF P_ROW IS INITIAL AND P_COL GT 0.

    MODIFY T_FIELDCAT FROM FS_FIELDCAT TRANSPORTING EMPHASIZE.

    ENDIF.

    ENDIF.

    ENDLOOP.

    FS_CELL-FIELDNAME = W_FIELDNAME .

    FS_CELL-COLOR-COL = 6.

    FS_CELL-NOKEYCOL = 'X'.

    APPEND FS_CELL TO T_SPFLI-CELL.

    IF P_ROW IS NOT INITIAL AND P_COL IS NOT INITIAL.

    MODIFY T_SPFLI INDEX P_ROW TRANSPORTING CELL.

    ENDIF.

    FS_LAYOUT-INFO_FIELDNAME = 'COLOR'.

    FS_LAYOUT-BOX_FIELDNAME = 'CHECKBOX'.

    FS_LAYOUT-COLTAB_FIELDNAME = 'CELL'.

    FS_LAYOUT-F2CODE = '&ETA'.

    W_PROG = SY-REPID.

    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

    EXPORTING

    I_CALLBACK_PROGRAM = W_PROG

    IS_LAYOUT = FS_LAYOUT

    IT_FIELDCAT = T_FIELDCAT

    TABLES

    T_OUTTAB = T_SPFLI

    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.

    NOTE

    Column and Row are colored with a coded color u2018Cxyzu2019.

    Where C: Color (coding must begin with C)

    X: Color Number

    Y: Bold

    Z: Inverse.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 02, 2008 at 11:17 AM
    Add comment
    10|10000 characters needed characters exceeded

  • Sep 02, 2008 at 11:18 AM
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 02, 2008 at 11:21 AM

    hi vidhya,

    here is your answer,

    REPORT z_colour NO STANDARD PAGE HEADING .

    TABLES :pa0002.

    TYPE-POOLS: slis. "ALV Declarations

    DATA : BEGIN OF it OCCURS 0,

    pernr LIKE pa0001-pernr,

    rufnm LIKE pa0002-rufnm,

    cell_colour TYPE lvc_t_scol, "Cell colour

    END OF it.

    SELECTION-SCREEN BEGIN OF BLOCK main WITH FRAME.

    SELECT-OPTIONS :s_pnum FOR pa0002-pernr .

    SELECTION-SCREEN END OF BLOCK main.

    *ALV data declarations

    DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH

    HEADER LINE,

    gd_layout TYPE slis_layout_alv,

    gd_repid LIKE sy-repid,

    gt_sort TYPE slis_t_sortinfo_alv.

    • To colour a cell.

    DATA ls_cellcolour TYPE lvc_s_scol.

    START-OF-SELECTION.

    PERFORM data_retrieval.

    PERFORM build_fieldcatalog.

    PERFORM build_layout.

    PERFORM display_alv_report.

    END-OF-SELECTION.

    FREE : it.

    FORM build_fieldcatalog .

    fieldcatalog-fieldname = 'PERNR'.

    fieldcatalog-seltext_m = 'Personnel No.'.

    fieldcatalog-col_pos = 0.

    APPEND fieldcatalog TO fieldcatalog.

    CLEAR fieldcatalog.

    fieldcatalog-fieldname = 'RUFNM'.

    fieldcatalog-seltext_m = 'Name'.

    fieldcatalog-col_pos = 0.

    APPEND fieldcatalog TO fieldcatalog.

    CLEAR fieldcatalog.

    ENDFORM.

    FORM build_layout .

    gd_layout-no_input = 'X'.

    gd_layout-colwidth_optimize = 'X'.

    gd_layout-totals_text = 'Totals'(256).

    gd_layout-coltab_fieldname = 'CELL_COLOUR'.

    ENDFORM. " build_layout

    FORM display_alv_report .

    gd_repid = sy-repid.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    i_callback_program = gd_repid

    is_layout = gd_layout

    it_fieldcat = fieldcatalog[]

    i_save = 'A'

    TABLES

    t_outtab = it

    EXCEPTIONS

    program_error = 1

    OTHERS = 2.

    IF sy-subrc <> 0.

    ENDIF.

    ENDFORM. " display_alv_report

    FORM data_retrieval .

    select pernr rufnm from pa0002 into corresponding

    fields of table it where pernr in s_pnum.

    LOOP AT it.

    *Now based on the value of the field pernr we can

    change the cell colour of the field rufnm or pernr.

    IF it-pernr eq '10001' .

    ls_cellcolour-fname = 'RUFNM'.

    ls_cellcolour-color-col = '5'.

    ls_cellcolour-color-int = '1'.

    ls_cellcolour-color-inv = '0'.

    APPEND ls_cellcolour TO it-cell_colour.

    IF sy-subrc EQ 0.

    MODIFY it.

    ENDIF.

    ENDIF.

    ENDLOOP.

    ENDFORM.

    best wishes.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 02, 2008 at 11:22 AM

    hi vidhya,

    here is your answer,

    REPORT z_colour NO STANDARD PAGE HEADING .

    TABLES :pa0002.

    TYPE-POOLS: slis. "ALV Declarations

    DATA : BEGIN OF it OCCURS 0,

    pernr LIKE pa0001-pernr,

    rufnm LIKE pa0002-rufnm,

    cell_colour TYPE lvc_t_scol, "Cell colour

    END OF it.

    SELECTION-SCREEN BEGIN OF BLOCK main WITH FRAME.

    SELECT-OPTIONS :s_pnum FOR pa0002-pernr .

    SELECTION-SCREEN END OF BLOCK main.

    *ALV data declarations

    DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH

    HEADER LINE,

    gd_layout TYPE slis_layout_alv,

    gd_repid LIKE sy-repid,

    gt_sort TYPE slis_t_sortinfo_alv.

    • To colour a cell.

    DATA ls_cellcolour TYPE lvc_s_scol.

    START-OF-SELECTION.

    PERFORM data_retrieval.

    PERFORM build_fieldcatalog.

    PERFORM build_layout.

    PERFORM display_alv_report.

    END-OF-SELECTION.

    FREE : it.

    FORM build_fieldcatalog .

    fieldcatalog-fieldname = 'PERNR'.

    fieldcatalog-seltext_m = 'Personnel No.'.

    fieldcatalog-col_pos = 0.

    APPEND fieldcatalog TO fieldcatalog.

    CLEAR fieldcatalog.

    fieldcatalog-fieldname = 'RUFNM'.

    fieldcatalog-seltext_m = 'Name'.

    fieldcatalog-col_pos = 0.

    APPEND fieldcatalog TO fieldcatalog.

    CLEAR fieldcatalog.

    ENDFORM.

    FORM build_layout .

    gd_layout-no_input = 'X'.

    gd_layout-colwidth_optimize = 'X'.

    gd_layout-totals_text = 'Totals'(256).

    gd_layout-coltab_fieldname = 'CELL_COLOUR'.

    ENDFORM. " build_layout

    FORM display_alv_report .

    gd_repid = sy-repid.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    i_callback_program = gd_repid

    is_layout = gd_layout

    it_fieldcat = fieldcatalog[]

    i_save = 'A'

    TABLES

    t_outtab = it

    EXCEPTIONS

    program_error = 1

    OTHERS = 2.

    IF sy-subrc <> 0.

    ENDIF.

    ENDFORM. " display_alv_report

    FORM data_retrieval .

    select pernr rufnm from pa0002 into corresponding

    fields of table it where pernr in s_pnum.

    LOOP AT it.

    *Now based on the value of the field pernr we can

    change the cell colour of the field rufnm or pernr.

    IF it-pernr eq '10001' .

    ls_cellcolour-fname = 'RUFNM'.

    ls_cellcolour-color-col = '5'.

    ls_cellcolour-color-int = '1'.

    ls_cellcolour-color-inv = '0'.

    APPEND ls_cellcolour TO it-cell_colour.

    IF sy-subrc EQ 0.

    MODIFY it.

    ENDIF.

    ENDIF.

    ENDLOOP.

    ENDFORM.

    best wishes.

    Add comment
    10|10000 characters needed characters exceeded