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: 

ALV Color a CELL?

Former Member
0 Kudos

HI all,

i need to place a color for a cell.

color is ORANGE.

i am passing the code 7 to that field.

but ia m getting PURPUL Color....

I ned to make it ORANGE..

ls_cellcolor-color-col = '7'.

ls_cellcolor-color-int = '0'.

ls_cellcolor-color-inv = '0'.

PLaese help...

sachin.

7 REPLIES 7

Former Member
0 Kudos

TABLES:LFA1.

SELECT-OPTIONS:LIFNR FOR LFA1-LIFNR.

DATA:BEGIN OF ITAB OCCURS 0,

LIFNR LIKE LFA1-LIFNR,

NAME1 LIKE LFA1-NAME1,

LAND1 LIKE LFA1-LAND1,

ORT01 LIKE LFA1-ORT01,

REGIO LIKE LFA1-REGIO,

SORTL LIKE LFA1-SORTL,

CFIELD(4) TYPE C,

END OF ITAB.

data:col(4).

data:num value '1'.

SELECT * FROM LFA1 INTO CORRESPONDING FIELDS OF TABLE ITAB WHERE LIFNR

IN LIFNR.

LOOP AT ITAB.

concatenate 'C' num '10' into col .

ITAB-CFIELD = col.

num = num + 1.

if num = '8'.

num = '1'.

endif.

MODIFY ITAB.

ENDLOOP.

TYPE-POOLS:SLIS.

DATA:FCAT TYPE SLIS_T_FIELDCAT_ALV.

DATA:LAYOUT TYPE SLIS_LAYOUT_ALV.

DATA:SORT TYPE slis_t_sortinfo_alv WITH HEADER LINE.

DATA:EVE TYPE SLIS_T_EVENT WITH HEADER LINE.

LAYOUT-COLWIDTH_OPTIMIZE = 'X'.

LAYOUT-WINDOW_TITLEBAR = 'VENDORS DETAILS SCREEN'.

LAYOUT-EDIT = 'X'.

LAYOUT-info_fieldname = 'CFIELD'.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_PROGRAM_NAME = SY-REPID

I_INTERNAL_TABNAME = 'ITAB'

I_INCLNAME = SY-REPID

CHANGING

CT_FIELDCAT = FCAT.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IS_LAYOUT = LAYOUT

IT_FIELDCAT = FCAT

TABLES

T_OUTTAB = ITAB.

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

this is for coloring cols

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

REPORT ZBHCOLOR_COLS.

TABLES:LFA1.

SELECT-OPTIONS:C_LIFNR FOR LFA1-LIFNR. " FOR GRID ONLY

PARAMETERS:LIST RADIOBUTTON GROUP ALV DEFAULT 'X',

GRID RADIOBUTTON GROUP ALV.

DATA:BEGIN OF ITAB OCCURS 0,

LIFNR LIKE LFA1-LIFNR,

NAME1 LIKE LFA1-NAME1,

LAND1 LIKE LFA1-LAND1,

ORT01 LIKE LFA1-ORT01,

SORTL LIKE LFA1-SORTL,

REGIO LIKE LFA1-REGIO,

COL TYPE LVC_T_SCOL,

END OF ITAB.

DATA:COLR TYPE LVC_S_SCOL.

SELECT * FROM LFA1 INTO CORRESPONDING FIELDS OF TABLE ITAB.

LOOP AT ITAB.

IF ITAB-LIFNR IN C_LIFNR.

COLR-FNAME = 'NAME1'.

COLR-COLOR-COL = '5'.

COLR-COLOR-INT = '1'.

COLR-COLOR-INV = '0'.

COLR-NOKEYCOL = 'X'.

APPEND COLR TO ITAB-COL.

COLR-FNAME = 'LIFNR'.

APPEND COLR TO ITAB-COL.

MODIFY ITAB.

ENDIF.

ENDLOOP.

TYPE-POOLS:SLIS.

DATA:FCAT TYPE SLIS_T_FIELDCAT_ALV.

DATA:LAYOUT TYPE SLIS_LAYOUT_ALV.

LAYOUT-ZEBRA = 'X'.

layout-coltab_fieldname = 'COL'.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_PROGRAM_NAME = SY-REPID

I_INTERNAL_TABNAME = 'ITAB'

I_INCLNAME = SY-REPID

CHANGING

CT_FIELDCAT = FCAT.

IF LIST = 'X'.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IS_LAYOUT = LAYOUT

IT_FIELDCAT = FCAT

TABLES

T_OUTTAB = ITAB.

ELSEIF GRID = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IS_LAYOUT = LAYOUT

IT_FIELDCAT = FCAT

TABLES

T_OUTTAB = ITAB.

ENDIF.

Reward if useful.

Former Member

Former Member
0 Kudos

hi sachin

can i know the way in which ur able to give a colour to a cell in ALV

if u can give some sample code then i can give the code for you

respond soon

regards

samarendra

0 Kudos

hi Samarendra

try like this

here is code

TYPE-POOLS:slis.

TABLES:mara,

makt,

marc.

DATA:BEGIN OF itab OCCURS 0,

matnr LIKE mara-matnr,

maktx LIKE makt-maktx,

werks LIKE marc-werks,

mtart LIKE mara-mtart,

matkl LIKE mara-matkl,

meins LIKE mara-meins,

ntgew LIKE mara-ntgew,

  • rowcolor(4) TYPE c,

cellcolors TYPE lvc_t_scol,

END OF itab.

DATA:t_fcat TYPE slis_t_fieldcat_alv,

t_eve TYPE slis_t_event.

DATA : st_layout TYPE slis_layout_alv.

SELECTION-SCREEN:BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

SELECT-OPTIONS:mat FOR mara-matnr. " no intervals no-extension.

*PARAMETERS:mat LIKE mara-matnr.

SELECTION-SCREEN:END OF BLOCK blk1.

INITIALIZATION.

PERFORM build_cata USING t_fcat.

PERFORM build_event.

START-OF-SELECTION.

PERFORM data_retrieval.

PERFORM display_data.

&----


*& Form build_cata

&----


  • text

----


  • -->TEMP_FCAT text

----


FORM build_cata USING temp_fcat TYPE slis_t_fieldcat_alv.

sy-tvar0 = sy-uname.

WRITE sy-datum TO sy-tvar1.

DATA:wa_fcat TYPE slis_fieldcat_alv.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'MATNR'.

  • wa_fcat-ref_tabname = 'MARA'.

  • wa_fcat-ref_fieldname = 'MATNR'.

wa_fcat-col_pos = 1.

wa_fcat-seltext_m = 'Material'.

  • wa_fcat-emphasize = 'C10'. "To change ALV Background Color

APPEND wa_fcat TO temp_fcat.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'MAKTX'.

wa_fcat-seltext_m = 'Description'.

wa_fcat-fix_column = 'x'.

wa_fcat-key = 'X'. "To color a column

APPEND wa_fcat TO temp_fcat.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'WERKS'.

wa_fcat-seltext_m = 'Plant'.

wa_fcat-key = ' '.

APPEND wa_fcat TO temp_fcat.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'MTART'.

wa_fcat-ddic_outputlen = 4.

wa_fcat-row_pos = 2.

  • wa_fcat-ref_tabname = 'MARA'.

  • wa_fcat-ref_fieldname = 'MTART'.

  • wa_fcat-seltext_m = 'Type'.

APPEND wa_fcat TO temp_fcat.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'MATKL'.

wa_fcat-ref_tabname = 'MARA'.

wa_fcat-row_pos = 2.

  • wa_fcat-ref_fieldname = 'MATKL'.

wa_fcat-seltext_m = 'Group'.

APPEND wa_fcat TO temp_fcat.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'MEINS'.

wa_fcat-ref_tabname = ' '.

wa_fcat-ref_fieldname = ' '.

wa_fcat-row_pos = 2.

wa_fcat-seltext_m = 'Measurement Unit'.

APPEND wa_fcat TO temp_fcat.

wa_fcat-tabname = 'ITAB'.

wa_fcat-fieldname = 'NTGEW'.

wa_fcat-seltext_m = 'Net Value'.

wa_fcat-row_pos = 2.

wa_fcat-no_out = ' '.

APPEND wa_fcat TO temp_fcat.

ENDFORM. "build_cata

&----


*& Form build_event

&----


  • text

----


FORM build_event.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

EXPORTING

i_list_type = 0

IMPORTING

et_events = t_eve

EXCEPTIONS

list_type_wrong = 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. "build_event

&----


*& Form data_retrieval

&----


  • text

----


FORM data_retrieval.

SELECT maramatnr maramtart maramatkl marameins mara~ntgew

maktmaktx marcwerks

INTO CORRESPONDING FIELDS OF TABLE itab

FROM mara INNER JOIN makt ON

maramatnr = maktmatnr

INNER JOIN marc ON

maramatnr = marcmatnr

WHERE mara~matnr IN mat.

SORT itab BY matnr.

DELETE ADJACENT DUPLICATES FROM itab.

ENDFORM. "data_retrieval

&----


*& Form display_data

&----


  • text

----


FORM display_data.

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

*******************************For setting Cell Color*******************************************

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

DATA ls_cellcolor TYPE lvc_s_scol .

st_layout-coltab_fieldname = 'CELLCOLORS'.

LOOP AT itab WHERE meins EQ 'KG'.

  • READ TABLE itab INDEX 5 .

ls_cellcolor-fname = 'MEINS' .

ls_cellcolor-color-col = '7' .

ls_cellcolor-color-int = '1' .

APPEND ls_cellcolor TO itab-cellcolors .

MODIFY itab TRANSPORTING cellcolors .

ENDLOOP.

  • st_layout-colwidth_optimize = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = 'ZALV_DS'

is_layout = st_layout

i_save = 'X'

it_fieldcat = t_fcat

it_events = t_eve

TABLES

t_outtab = itab

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_data

Former Member
0 Kudos

hi,

i am passing this

ls_cellcolor-color-col = '7' .

ls_cellcolor-color-int = '1' .

i am getting orange colour.....

reward if usefull.

Former Member
0 Kudos

Hi,

Please refer to the link below :

http://www.saptechnical.com/Tutorials/ALV/cell/highlight.htm

Thanks,

Sriram Ponna.

Former Member
0 Kudos

HI,

C.6.2. Coloring an Entire Row

Coloring a row is a bit (really a bit) more complicated. To enable row coloring, you should add an additional field to your list data table. It should be of character type and length at least 4. This field will contain the color code for the row. So, let’s modify declaration of our list data table “gt_list”.

*--- Internal table holding list data

DATA BEGIN OF gt_list OCCURS 0 .

INCLUDE STRUCTURE SFLIGHT .

DATA rowcolor(4) TYPE c .

DATA END OF gt_list .

Code Part 13 – Adding the field that will contain row color data

As you guess, you should fill the color code to this field. Its format will be the same as explained before at section C.6.3. But how will ALV Grid know that you have loaded the color data for the row to this field. So, you make it know this by

passing the name of the field containing color codes to the field “INFO_FNAME” of the layout structure.

e.g.

ps_layout-info_fname = <field_name_containing_color_codes>. “e.g. ‘ROWCOLOR’

You can fill that field anytime during execution. But, of course, due to the flow logic of screens, it will be reflected to your list display as soon as an ALV refresh occurs.

You can color an entire row as described in the next section. However, this method is less time consuming.

C.6.3. Coloring Individual Cells

This is the last point about coloring procedures for the ALV Grid. The procedure is similar to coloring an entire row. However, since an individual cell can be addressed with two parameters we will need something more. What is meant by “more” is a table type structure to be included into the structure of the list data table. It seems strange, because including it will make our list data structure deep. But anyhow ALV Grid control handles this.

The structure that should be included must be of type “LVC_T_SCOL”. If you want to color the entire row, this inner table should contain only one row with field “fname” is set to space, some color value at field “col”, “0” or “1” at fields “int” (intensified) and “inv” (inverse).

If you want to color individual cells, then for each cell column, append a line to this inner table which also contains the column name at field “fname”. It is obvious that you can color an entire column by filling this inner table with a row for that column for each row in the list data table. But, it is also obvious that, this will be more time consuming than the method at section C.6.1.

Again key field coloring will override your settings. That’s why, we have another field in this inner table called “nokeycol”. For each field represented in the inner table, set this field to ‘X’ to prevent overriding of key color settings.

In this procedure, again we must tell the control the name of the inner table containing color data. The field “CTAB_FNAME” of the layout structure is used for this purpose.

Code Part 14 – Adding inner table that will contain cell color data

*--- Internal table holding list data

DATA BEGIN OF gt_list OCCURS 0 .

INCLUDE STRUCTURE SFLIGHT .

DATA rowcolor(4) TYPE c .

DATA cellcolors TYPE lvc_t_scol .

DATA END OF gt_list .

Code Part 15 – A sample code to make the cell at row 5 and column ‘SEATSOCC’ colored

DATA ls_cellcolor TYPE lvc_s_scol .

...

READ TABLE gt_list INDEX 5 .

ls_cellcolor-fname = 'SEATSOCC' .

ls_cellcolor-color-col = '7' .

ls_cellcolor-color-int = '1' .

APPEND ls_cellcolor TO gt_list-cellcolors .

MODIFY gt_list INDEX 5 .

A

Please reward if helpful

cheers ,

SreekarKadiri.

Edited by: sreekar kadiri on Feb 1, 2008 12:34 PM