Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Coloring a Particular Column of an ALV Output

Former Member
0 Kudos

Hi,

I have an ALV Output. I want to color one Particular Column conditionally based on the fieldvalue.

If Fieldvalue = '1'.

I want the cell to be colored in Red.

If Fieldvalue = '2'.

I want the cell to be colored in Yellow.

If Fieldvalue = '3'.

I want the cell to be colored Green.

So, in the output I will be having Red, Yellow or Green color in the same column but different records.

Fieldvalue is a Field in my final internal table.

Please help resolve.

Thanks in advance,

Ishaq.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Ishaq,

Check these statements.

Loop the output internal table and write the following statements

If Fieldvalue = '1'.

CLEAR ls_tabcolor.

ls_tabcolor-fname = 'Fieldvalue'.

ls_tabcolor-color-col = 1. " Blue.

ls_tabcolor-color-int = 1. " Intensified on

ls_tabcolor-color-inv = 1. " Inverse on

INSERT ls_tabcolor INTO TABLE w_output-tabcolor.

like this set the desired color based on the condition and to the particular field.

I am sending you the sample code for displaying the particular field in a record in a particular record.

Please refer to this, it will be helpful to you.


REPORT zmk_alv_lvc.
TABLES: vbak.

SELECTION-SCREEN: BEGIN OF BLOCK b1.
SELECT-OPTIONS: cust FOR vbak-kunnr.
SELECTION-SCREEN: END OF BLOCK b1.

TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
ernam TYPE ernam,
netwr TYPE netwr_ak,
END OF ty_vbak.

TYPES: BEGIN OF ty_vbak1,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
ernam TYPE ernam,
netwr TYPE netwr_ak,
tabcolor TYPE lvc_t_scol,
END OF ty_vbak1.

DATA: i_vbak TYPE TABLE OF ty_vbak,
w_vbak TYPE ty_vbak,
i_fieldcat TYPE lvc_t_fcat, "slis_t_fieldcat_alv,
i_vbak1 TYPE TABLE OF ty_vbak1,
w_vbak1 TYPE ty_vbak1.
DATA: ls_layout TYPE lvc_s_layo.

SELECT vbeln erdat ernam netwr FROM vbak INTO TABLE i_vbak WHERE kunnr IN cust.

PERFORM fieldcat.
PERFORM display.
&---------------------------------------------------------------------
*& Form fieldcat
&---------------------------------------------------------------------
FORM fieldcat .
DATA: w_fieldcat TYPE LINE OF lvc_t_fcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'VBELN'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'DOCUMENT NUMBER'.
APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'ERDAT'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'CREATED ON'.
APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'ERNAM'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'CREATED BY'.
APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'NETWR'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'NET PRICE'.
APPEND w_fieldcat TO i_fieldcat.

ENDFORM. " fieldcat
&---------------------------------------------------------------------
*& Form display
&---------------------------------------------------------------------
FORM display .
DATA: ls_tabcolor TYPE lvc_s_scol.

LOOP AT i_vbak INTO w_vbak.
IF sy-tabix <> 3.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'VBELN'.
ls_tabcolor-color-col = 1. " Blue.
ls_tabcolor-color-int = 1. " Intensified on
ls_tabcolor-color-inv = 1. " Inverse on
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERDAT'.
ls_tabcolor-color-col = 2. " Blue.
ls_tabcolor-color-int = 0. " Intensified off
ls_tabcolor-color-inv = 0. " Inverse off
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERNAM'.
ls_tabcolor-color-col = 3. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'NETWR'.
ls_tabcolor-color-col = 4. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.

MOVE-CORRESPONDING w_vbak TO w_vbak1.
APPEND w_vbak1 TO i_vbak1.
ELSE.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'VBELN'.
ls_tabcolor-color-col = 1. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERDAT'.
ls_tabcolor-color-col = 2. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERNAM'.
ls_tabcolor-color-col = 3. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'NETWR'.
ls_tabcolor-color-col = 6. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.

MOVE-CORRESPONDING w_vbak TO w_vbak1.
APPEND w_vbak1 TO i_vbak1.
ENDIF.
CLEAR w_vbak.
CLEAR w_vbak1.
ENDLOOP.

ls_layout-ctab_fname = 'TABCOLOR'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
TABLES
t_outtab = i_vbak1.
ENDFORM. " display

Reward points if helpful.

Thanks,

Khan.

2 REPLIES 2

Former Member
0 Kudos

Hi,

Check the below threads:

Thanks.

Former Member
0 Kudos

Hi Ishaq,

Check these statements.

Loop the output internal table and write the following statements

If Fieldvalue = '1'.

CLEAR ls_tabcolor.

ls_tabcolor-fname = 'Fieldvalue'.

ls_tabcolor-color-col = 1. " Blue.

ls_tabcolor-color-int = 1. " Intensified on

ls_tabcolor-color-inv = 1. " Inverse on

INSERT ls_tabcolor INTO TABLE w_output-tabcolor.

like this set the desired color based on the condition and to the particular field.

I am sending you the sample code for displaying the particular field in a record in a particular record.

Please refer to this, it will be helpful to you.


REPORT zmk_alv_lvc.
TABLES: vbak.

SELECTION-SCREEN: BEGIN OF BLOCK b1.
SELECT-OPTIONS: cust FOR vbak-kunnr.
SELECTION-SCREEN: END OF BLOCK b1.

TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
ernam TYPE ernam,
netwr TYPE netwr_ak,
END OF ty_vbak.

TYPES: BEGIN OF ty_vbak1,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
ernam TYPE ernam,
netwr TYPE netwr_ak,
tabcolor TYPE lvc_t_scol,
END OF ty_vbak1.

DATA: i_vbak TYPE TABLE OF ty_vbak,
w_vbak TYPE ty_vbak,
i_fieldcat TYPE lvc_t_fcat, "slis_t_fieldcat_alv,
i_vbak1 TYPE TABLE OF ty_vbak1,
w_vbak1 TYPE ty_vbak1.
DATA: ls_layout TYPE lvc_s_layo.

SELECT vbeln erdat ernam netwr FROM vbak INTO TABLE i_vbak WHERE kunnr IN cust.

PERFORM fieldcat.
PERFORM display.
&---------------------------------------------------------------------
*& Form fieldcat
&---------------------------------------------------------------------
FORM fieldcat .
DATA: w_fieldcat TYPE LINE OF lvc_t_fcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'VBELN'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'DOCUMENT NUMBER'.
APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'ERDAT'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'CREATED ON'.
APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'ERNAM'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'CREATED BY'.
APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.
w_fieldcat-fieldname = 'NETWR'.
w_fieldcat-tabname = 'I_VBAK'.
w_fieldcat-seltext = 'NET PRICE'.
APPEND w_fieldcat TO i_fieldcat.

ENDFORM. " fieldcat
&---------------------------------------------------------------------
*& Form display
&---------------------------------------------------------------------
FORM display .
DATA: ls_tabcolor TYPE lvc_s_scol.

LOOP AT i_vbak INTO w_vbak.
IF sy-tabix <> 3.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'VBELN'.
ls_tabcolor-color-col = 1. " Blue.
ls_tabcolor-color-int = 1. " Intensified on
ls_tabcolor-color-inv = 1. " Inverse on
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERDAT'.
ls_tabcolor-color-col = 2. " Blue.
ls_tabcolor-color-int = 0. " Intensified off
ls_tabcolor-color-inv = 0. " Inverse off
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERNAM'.
ls_tabcolor-color-col = 3. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'NETWR'.
ls_tabcolor-color-col = 4. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.

MOVE-CORRESPONDING w_vbak TO w_vbak1.
APPEND w_vbak1 TO i_vbak1.
ELSE.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'VBELN'.
ls_tabcolor-color-col = 1. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERDAT'.
ls_tabcolor-color-col = 2. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'ERNAM'.
ls_tabcolor-color-col = 3. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.
CLEAR ls_tabcolor.
ls_tabcolor-fname = 'NETWR'.
ls_tabcolor-color-col = 6. " Blue.
ls_tabcolor-color-int = 0.
ls_tabcolor-color-inv = 0.
INSERT ls_tabcolor INTO TABLE w_vbak1-tabcolor.

MOVE-CORRESPONDING w_vbak TO w_vbak1.
APPEND w_vbak1 TO i_vbak1.
ENDIF.
CLEAR w_vbak.
CLEAR w_vbak1.
ENDLOOP.

ls_layout-ctab_fname = 'TABCOLOR'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
TABLES
t_outtab = i_vbak1.
ENDFORM. " display

Reward points if helpful.

Thanks,

Khan.