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 grid

Former Member
0 Kudos

In ALV grid output how do we change the colour of a particular row?(or how do we provide different colour to different rows).is there any provision for that?Need some info on that.

7 REPLIES 7

amit_khare
Active Contributor
0 Kudos

Former Member
0 Kudos

Former Member
0 Kudos

hi,

Here is some colors and fonting:

REPORT ZR_EXCEL_WITH_COLORS.

  • Export to Excel data defs - below

INCLUDE OLE2INCL.

  • handles for OLE objects

DATA: hExcel TYPE OLE2_OBJECT, " Excel object

hWorkBooks TYPE OLE2_OBJECT, " list of workbooks

hWorkbook TYPE OLE2_OBJECT, " workbook

hSheet TYPE OLE2_OBJECT, " worksheet object

hRange TYPE OLE2_OBJECT, " range object

hRange2 TYPE OLE2_OBJECT, " range object

hBorders TYPE OLE2_OBJECT, " Border object

hInterior TYPE OLE2_OBJECT, " interior object - for coloring

hColumn TYPE OLE2_OBJECT, "column

hCell TYPE OLE2_OBJECT, " cell

hFont TYPE OLE2_OBJECT, " font

hSelected TYPE OLE2_OBJECT, " range object

hPicture TYPE OLE2_OBJECT, "picture object

hLogo TYPE OLE2_OBJECT. "Logo object

DATA H TYPE I.

constants: xlCenter type i value '-4108',

xlBottom type i value '-4107',

xlLeft type i value '-4131',

xlRight type i value '-4152'.

constants: xlMinimized type i value '-4140'.

constants: xlContinuous type i value '1',

xlInsideVertical type i value '11',

xlThin type i value '2',

xlLandscape type i value '2',

xlPortrait type i value '1',

xlLetter type i value '1',

xlLegal type i value '5',

xlThick type i value '4',

xlNone type i value '-4142',

xlAutomatic type i value '-4105'.

start-of-selection.

Perform Start_Excel using 'c:\my_file.xls'.

Perform Build_Chain_Line.

Perform Release_Excel.

FORM Start_Excel using p_CliFile.

CREATE OBJECT hExcel 'EXCEL.APPLICATION'.

PERFORM ERR_HDL.

  • get list of workbooks, initially empty

CALL METHOD OF hExcel 'Workbooks' = hWorkbooks.

PERFORM ERR_HDL.

  • add a new workbook

CALL METHOD OF hWorkbooks 'Add' = hWorkbook.

PERFORM ERR_HDL.

  • Get Worksheet object.

get property of hWorkbook 'ActiveSheet' = hSheet.

ENDFORM. " Start_Excel

FORM Build_Chain_Line.

data: Chain_Name(40).

data: Year_Line(30).

data: p_row_cnt type i.

p_row_cnt = 1.

Chain_Name = 'My Chain Here'.

  • Select range object onto cell A1.

CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = 'A1:A1'.

*Add the chain name to A2.

PERFORM Fill_The_Cell USING p_row_cnt 1 1 Chain_Name.

  • Set background color to tan.

call method of hRange 'Interior' = hInterior.

set property of hInterior 'ColorIndex' = 40. "tan

*Add the year to A3.

Year_Line = '2007'.

p_row_cnt = p_row_cnt + 1.

PERFORM Fill_The_Cell USING p_row_cnt 1 1 Year_Line.

  • Select range object onto cell A2.

CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = 'A2:A2'.

  • Set background color to blue.

call method of hRange 'Interior' = hInterior.

set property of hInterior 'ColorIndex' = 20. "blue

  • Center the chain name.

set property of hRange 'HorizontalAlignment' = xlCenter.

set property of hRange 'VerticalAlignment' = xlCenter.

  • Change font to 14 point.

call method of hRange 'Font' = hFont.

set property of hFont 'Size' = 14.

Endform.

FORM Release_Excel .

CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = 'A6'.

call method of hRange 'Select'.

  • Set Excel VISIBLE property to TRUE.

  • set property of hExcel 'WindowState' = xlMinimized.

SET PROPERTY OF hExcel 'Visible' = 1.

FREE OBJECT hExcel.

PERFORM ERR_HDL.

ENDFORM. " Release_Excel

----


  • FORM Fill_The_Cell *

----


  • Sets cell at coordinates i,j to value val boldtype bold *

*

  • BOLD --> 1 = true, set bold ON 0 = false, set bold OFF

----


FORM Fill_The_Cell USING I J BOLD TheValue.

CALL METHOD OF hExcel 'Cells' = hCell EXPORTING #1 = I #2 = J.

PERFORM ERR_HDL.

SET PROPERTY OF hCell 'Value' = TheValue.

PERFORM ERR_HDL.

GET PROPERTY OF hCell 'Font' = hFont.

PERFORM ERR_HDL.

SET PROPERTY OF hFont 'Bold' = BOLD.

PERFORM ERR_HDL.

ENDFORM.

*&----


*& Form ERR_HDL

*&----


  • outputs OLE error if any

*----


  • --> p1 text

  • <-- p2 text

*----


FORM ERR_HDL.

IF SY-SUBRC <> 0.

message i000(zz) with 'OLE Automation error: ' SY-SUBRC.

exit.

ENDIF.

ENDFORM. " ERR_HDL

this one is already posted in Forum..I am copying and pasting here.

Pls reward if it is helpful

Regards,

Sangeetha.A

Former Member
0 Kudos

HI Savitha

Just go through this

http://help.sap.com/saphelp_47x200/helpdata/en/7f/e477e2fba211d2b48f006094192fe3/frameset.htm

and

this is the sample program

report zrich_0002 .

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

  • Use of colours in ALV grid (cell, line and column) *

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

  • Table

tables : mara.

  • Type

types : begin of ty_mara,

matnr like mara-matnr,

matkl like mara-matkl,

counter(4) type n,

free_text(15) type c,

color_line(4) type c, " Line color

color_cell type lvc_t_scol, " Cell color

end of ty_mara.

  • Structures

data : wa_mara type ty_mara,

wa_fieldcat type lvc_s_fcat,

is_layout type lvc_s_layo,

wa_color type lvc_s_scol.

  • Internal table

data : it_mara type standard table of ty_mara,

it_fieldcat type standard table of lvc_s_fcat,

it_color type table of lvc_s_scol.

  • Variables

data : okcode like sy-ucomm,

w_alv_grid type ref to cl_gui_alv_grid,

w_docking_container type ref to cl_gui_docking_container.

parameters : p_column as checkbox,

p_line as checkbox,

p_cell as checkbox.

at selection-screen output.

perform get_data.

perform fill_catalog.

if w_docking_container is initial.

perform create_objects.

endif.

&----


*& Form create_objects

&----


form create_objects.

create object w_docking_container

exporting

ratio = 60

exceptions

cntl_error = 1

cntl_system_error = 2

create_error = 3

lifetime_error = 4

lifetime_dynpro_dynpro_link = 5

others = 6.

create object w_alv_grid

exporting

i_parent = w_docking_container.

  • Field that identify color line in internal table

move 'COLOR_LINE' to is_layout-info_fname.

  • Field that identify cell color in inetrnal table

move 'COLOR_CELL' to is_layout-ctab_fname.

call method w_alv_grid->set_table_for_first_display

exporting

is_layout = is_layout

changing

it_outtab = it_mara

it_fieldcatalog = it_fieldcat

exceptions

invalid_parameter_combination = 1

program_error = 2

too_many_lines = 3

others = 4.

endform.

&----


*& Form get_data

&----


form get_data.

select * from mara up to 5 rows.

clear : wa_mara-color_line, wa_mara-color_cell.

move-corresponding mara to wa_mara.

add 1 to wa_mara-counter.

move 'Blabla' to wa_mara-free_text.

if wa_mara-counter = '0002'

and p_line = 'X'.

  • Color line

move 'C410' to wa_mara-color_line.

elseif wa_mara-counter = '0004'

and p_cell = 'X'.

  • Color cell

move 'FREE_TEXT' to wa_color-fname.

move '6' to wa_color-color-col.

move '1' to wa_color-color-int.

move '1' to wa_color-color-inv.

append wa_color to it_color.

wa_mara-color_cell[] = it_color[].

endif.

append wa_mara to it_mara.

endselect.

endform.

&----


*& Form fill_catalog

&----


form fill_catalog.

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

  • Colour code : *

  • Colour is a 4-char field where : *

  • - 1st char = C (color property) *

  • - 2nd char = color code (from 0 to 7) *

  • 0 = background color *

  • 1 = blue *

  • 2 = gray *

  • 3 = yellow *

  • 4 = blue/gray *

  • 5 = green *

  • 6 = red *

  • 7 = orange *

  • - 3rd char = intensified (0=off, 1=on) *

  • - 4th char = inverse display (0=off, 1=on) *

  • *

  • Colour overwriting priority : *

  • 1. Line *

  • 2. Cell *

  • 3. Column *

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

data : w_position type i value '1'.

clear wa_fieldcat.

move w_position to wa_fieldcat-col_pos.

move 'MATNR' to wa_fieldcat-fieldname.

move 'MARA' to wa_fieldcat-ref_table.

move 'MATNR' to wa_fieldcat-ref_field.

append wa_fieldcat to it_fieldcat.

add 1 to w_position.

clear wa_fieldcat.

move w_position to wa_fieldcat-col_pos.

move 'MATKL' to wa_fieldcat-fieldname.

move 'MARA' to wa_fieldcat-ref_table.

move 'MATKL' to wa_fieldcat-ref_field.

  • Color column

if p_column = 'X'.

move 'C610' to wa_fieldcat-emphasize.

endif.

append wa_fieldcat to it_fieldcat.

add 1 to w_position.

clear wa_fieldcat.

move w_position to wa_fieldcat-col_pos.

move 'COUNTER' to wa_fieldcat-fieldname.

move 'N' to wa_fieldcat-inttype.

move '4' to wa_fieldcat-intlen.

move 'Counter' to wa_fieldcat-coltext.

append wa_fieldcat to it_fieldcat.

add 1 to w_position.

clear wa_fieldcat.

move w_position to wa_fieldcat-col_pos.

move 'FREE_TEXT' to wa_fieldcat-fieldname.

move 'C' to wa_fieldcat-inttype.

move '20' to wa_fieldcat-intlen.

move 'Text' to wa_fieldcat-coltext.

append wa_fieldcat to it_fieldcat.

endform.

If it helps reward with points

Regards Rk

Message was edited by:

Rk Pasupuleti

Former Member
0 Kudos

call the type pools slis

in that u've got a stucture of type alv_grid_layout_spec

in that there is a field name called zebra.. something like that

if u invoke it u'll be getting the rows in alternate colours

Former Member
0 Kudos

hi,

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”.

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.

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.

TYPE-POOLS: SLIS, ICON.

DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.

DATA: BEGIN OF IMARA OCCURS 0,

LIGHT(4) TYPE C,

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MAKTX TYPE MAKT-MAKTX,

COLOR_LINE(4) TYPE C,

TCOLOR TYPE SLIS_T_SPECIALCOL_ALV, "cell

END OF IMARA.

DATA: XCOLOR TYPE SLIS_SPECIALCOL_ALV.

START-OF-SELECTION.

PERFORM GET_DATA.

PERFORM WRITE_REPORT.

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

  • Get_Data

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

FORM GET_DATA.

WRITE ICON_GREEN_LIGHT AS ICON TO IMARA-LIGHT.

IMARA-MATNR = 'ABC'.

IMARA-MTART = 'ZCFG'.

IMARA-MAKTX = 'This is description for ABC'.

APPEND IMARA.

WRITE ICON_YELLOW_LIGHT AS ICON TO IMARA-LIGHT.

IMARA-MATNR = 'DEF'.

IMARA-MTART = 'ZCFG'.

IMARA-MAKTX = 'This is description for DEF'.

APPEND IMARA.

WRITE ICON_RED_LIGHT AS ICON TO IMARA-LIGHT.

IMARA-MATNR = 'GHI'.

IMARA-MTART = 'ZCFG'.

IMARA-MAKTX = 'This is description for GHI'.

APPEND IMARA.

LOOP AT IMARA.

IF SY-TABIX = 1.

IMARA-COLOR_LINE = 'C410'. " color line

ENDIF.

IF SY-TABIX = 2. " color CELL

CLEAR XCOLOR.

XCOLOR-FIELDNAME = 'MTART'.

XCOLOR-COLOR-COL = '3'.

XCOLOR-COLOR-INT = '1'. " Intensified on/off

XCOLOR-COLOR-INV = '0'.

APPEND XCOLOR TO IMARA-TCOLOR.

ENDIF.

MODIFY IMARA.

ENDLOOP.

ENDFORM. "get_data

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

  • WRITE_REPORT

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

FORM WRITE_REPORT.

DATA: LAYOUT TYPE SLIS_LAYOUT_ALV.

LAYOUT-COLTAB_FIELDNAME = 'TCOLOR'.

LAYOUT-INFO_FIELDNAME = 'COLOR_LINE'.

PERFORM BUILD_FIELD_CATALOG.

  • CALL ABAP LIST VIEWER (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IS_LAYOUT = LAYOUT

IT_FIELDCAT = FIELDCAT

TABLES

T_OUTTAB = IMARA.

ENDFORM. "write_report

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

  • BUILD_FIELD_CATALOG

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

FORM BUILD_FIELD_CATALOG.

DATA: FC_TMP TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.

CLEAR: FIELDCAT. REFRESH: FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Status'.

FC_TMP-FIELDNAME = 'LIGHT'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '4'.

FC_TMP-ICON = 'X'.

APPEND FC_TMP TO FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Material Number'.

FC_TMP-FIELDNAME = 'MATNR'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '18'.

APPEND FC_TMP TO FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Material Type'.

FC_TMP-FIELDNAME = 'MTART'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '10'.

APPEND FC_TMP TO FIELDCAT.

CLEAR: FC_TMP.

FC_TMP-REPTEXT_DDIC = 'Material'.

FC_TMP-FIELDNAME = 'MAKTX'.

FC_TMP-TABNAME = 'IMARA'.

FC_TMP-OUTPUTLEN = '40'.

FC_TMP-EMPHASIZE = 'C610'. " color column

APPEND FC_TMP TO FIELDCAT.

ENDFORM. "build_field_catalog

reward points if useful.

regards,

kiran kumar k.

Message was edited by:

kiran kumar

Former Member
0 Kudos

Hi savitha,

try to analyze this code and execute in your program

constants :

gray type i value 1,

yellow type i value 3,

blue type i value 4,

green type i value 5,

red type i value 6,

orange type i value 7,

lblue type i value 2.

form set_color.

loop at tab.

if not tab-objnr is initial.

perform field_color_set using 'KOKRS' blue.

perform field_color_set using 'KWAER' blue.

perform field_color_set using 'WERKS' blue.

perform field_color_set using 'VBELN' blue.

perform field_color_set using 'POSNR' blue.

perform field_color_set using 'AUFNR' blue.

perform field_color_set using 'OBJNR' blue.

perform field_color_set using 'MATNR' yellow.

perform field_color_set using 'MEINS' yellow.

perform field_color_set using 'MAKTX' yellow.

perform field_color_set using 'WEMNG' yellow.

perform field_color_set using 'AMEIN' green.

perform field_color_set using 'KALAB' green.

perform field_color_set using 'KAINS' green.

perform field_color_set using 'KASPE' green.

perform field_color_set using 'WKGBTR' gray.

perform field_color_set using 'ZWKGXXX_ACT' gray.

perform field_color_set using 'ZWKGXXX_PACK' gray.

perform field_color_set using 'ZWKGXXX_OHIS' gray.

perform field_color_set using 'ZWKGXXX_OHNS' gray.

perform field_color_set using 'TWAER' orange.

perform field_color_set using 'IDNRK' orange.

perform field_color_set using 'I_MBGBTR' orange.

perform field_color_set using 'I_MEINB' orange.

perform field_color_set using 'I_WKGBTR' orange.

perform field_color_set using 'ZS_MATNR' red.

perform field_color_set using 'ZS_MBGXXX' red.

perform field_color_set using 'ZS_MEINB' red.

perform field_color_set using 'ZS_VALUE' red.

perform field_color_set2 using 'ZFG_VALUE' lblue.

perform field_color_set2 using 'ZFG_QTY' lblue.

perform field_color_set2 using 'ZWP_VALUE' lblue.

perform field_color_set using 'ZWP_QTY' lblue.

perform field_color_set using 'ZICON_AL1' lblue.

else.

perform field_color_set using 'KOKRS' blue.

perform field_color_set using 'KWAER' blue.

perform field_color_set using 'WERKS' blue.

perform field_color_set using 'VBELN' blue.

perform field_color_set using 'POSNR' blue.

perform field_color_set using 'AUFNR' blue.

perform field_color_set using 'OBJNR' blue.

perform field_color_set using 'MATNR' yellow.

perform field_color_set using 'MEINS' yellow.

perform field_color_set using 'MAKTX' yellow.

perform field_color_set using 'WEMNG' yellow.

perform field_color_set using 'AMEIN' yellow.

perform field_color_set using 'KALAB' yellow.

perform field_color_set using 'KAINS' yellow.

perform field_color_set using 'KASPE' yellow.

perform field_color_set using 'WKGBTR' yellow.

perform field_color_set using 'ZWKGXXX_ACT' yellow.

perform field_color_set using 'ZWKGXXX_PACK' yellow.

perform field_color_set using 'ZWKGXXX_OHIS' yellow.

perform field_color_set using 'ZWKGXXX_OHNS' yellow.

perform field_color_set using 'TWAER' yellow.

perform field_color_set using 'IDNRK' yellow.

perform field_color_set using 'I_MBGBTR' yellow.

perform field_color_set using 'I_MEINB' yellow.

perform field_color_set using 'I_WKGBTR' yellow.

perform field_color_set using 'ZS_MATNR' yellow.

perform field_color_set using 'ZS_MBGXXX' yellow.

perform field_color_set using 'ZS_MEINB' yellow.

perform field_color_set using 'ZS_VALUE' yellow.

perform field_color_set using 'ZFG_VALUE' yellow.

perform field_color_set using 'ZFG_QTY' yellow.

perform field_color_set using 'ZWP_VALUE' yellow.

perform field_color_set using 'ZWP_QTY' yellow.

perform field_color_set using 'ZICON_AL1' yellow.

endif.

modify tab.

endloop.

endform. " set_color

&----


*& Form field_color_set

&----


  • text

----


  • -->P_1003 text

  • -->P_COULEUR_COURANTE text

----


form field_color_set using nom couleur.

data : lp_color type kkblo_specialcol.

clear : lp_color.

lp_color-fieldname = nom.

lp_color-color-int = 0.

lp_color-color-inv = 0.

lp_color-nokeycol = 'X'.

lp_color-color-col = couleur.

append lp_color to tab-color.

endform.

&----


*& Form field_color_set

&----


  • text

----


  • -->P_1003 text

  • -->P_COULEUR_COURANTE text

----


form field_color_set2 using nom couleur.

data : lp_color type kkblo_specialcol.

clear : lp_color.

lp_color-fieldname = nom.

lp_color-color-int = 0.

lp_color-color-inv = 1.

lp_color-nokeycol = 'X'.

lp_color-color-col = couleur.

append lp_color to tab-color.

endform.