2007 Mar 02 8:57 AM
Hi,
Does anyone knows how to change the text color in the ALV for certain cells only which satisfy certain condition? If possible, provide me some sample codes will be good.
Thanks a lot in advance.
Lawrence
2007 Mar 02 9:00 AM
https://forums.sdn.sap.com/click.jspa?searchID=1423708&messageID=2882709
hi check the above thread which exactly talks of changing only some cells to be colored
dont forget to award points if found helpful
I dont know the reason why the link isnt working, i am pasting the code from there
Define a column as follows in output table type
TYPES : BEGIN OF ty_output.
INCLUDE STRUCTURE MARA.
TYPES :
For cell coloring
cellcolor TYPE lvc_t_scol,
END OF ty_output.
data : w_cellcolor TYPE lvc_s_scol, "For cell color
Final output table
itab2 TYPE STANDARD TABLE OF ty_output,
wa2 TYPE ty_output,
w_layout TYPE lvc_s_layo.
2. Colouring cell
CLEAR wa2.
READ TABLE itab2 INTO wa2 INDEX 7.
IF sy-subrc EQ 0.
w_cellcolor-fname = 'ERSDA'.
w_cellcolor-color-col = '7'.
w_cellcolor-color-int = '1'.
w_cellcolor-color-inv = '1'.
APPEND w_cellcolor TO wa2-cellcolor.
MODIFY itab2 FROM wa2 TRANSPORTING cellcolor WHERE matnr = wa2-matnr.
ENDIF.
3. w_layout-ctab_fname = 'CELLCOLOR'."For cell coloring
4. CALL METHOD o_grid->set_table_for_first_display
EXPORTING
I_STRUCTURE_NAME = 'MARA'
IS_VARIANT = w_variant
I_SAVE = 'A'
it_toolbar_excluding = i_exclude
is_layout = w_layout
CHANGING
it_outtab = itab2
it_fieldcatalog = i_fieldcat.
2007 Mar 02 8:59 AM
2007 Mar 02 9:00 AM
https://forums.sdn.sap.com/click.jspa?searchID=1423708&messageID=2882709
hi check the above thread which exactly talks of changing only some cells to be colored
dont forget to award points if found helpful
I dont know the reason why the link isnt working, i am pasting the code from there
Define a column as follows in output table type
TYPES : BEGIN OF ty_output.
INCLUDE STRUCTURE MARA.
TYPES :
For cell coloring
cellcolor TYPE lvc_t_scol,
END OF ty_output.
data : w_cellcolor TYPE lvc_s_scol, "For cell color
Final output table
itab2 TYPE STANDARD TABLE OF ty_output,
wa2 TYPE ty_output,
w_layout TYPE lvc_s_layo.
2. Colouring cell
CLEAR wa2.
READ TABLE itab2 INTO wa2 INDEX 7.
IF sy-subrc EQ 0.
w_cellcolor-fname = 'ERSDA'.
w_cellcolor-color-col = '7'.
w_cellcolor-color-int = '1'.
w_cellcolor-color-inv = '1'.
APPEND w_cellcolor TO wa2-cellcolor.
MODIFY itab2 FROM wa2 TRANSPORTING cellcolor WHERE matnr = wa2-matnr.
ENDIF.
3. w_layout-ctab_fname = 'CELLCOLOR'."For cell coloring
4. CALL METHOD o_grid->set_table_for_first_display
EXPORTING
I_STRUCTURE_NAME = 'MARA'
IS_VARIANT = w_variant
I_SAVE = 'A'
it_toolbar_excluding = i_exclude
is_layout = w_layout
CHANGING
it_outtab = itab2
it_fieldcatalog = i_fieldcat.
2007 Mar 02 9:06 AM
2007 Mar 02 9:07 AM
<b>SCREEN 100.
CUSTON CONTROL - ALV
BACK BUTTON - BACK</b>
REPORT zex35 .
****************************************************************
Use of colours in ALV grid (cell, line and column) *
****************************************************************
TABLES : mara.
TYPES : BEGIN OF ty_mara,
matnr LIKE mara-matnr,
kunnr LIKE mara-kunnr,
value TYPE i,
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.
DATA : wa_mara TYPE ty_mara,
wa_fieldcat TYPE lvc_s_fcat,
is_layout TYPE lvc_s_layo,
wa_color TYPE lvc_s_scol.
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.
DATA : okcode LIKE sy-ucomm,
w_alv_grid TYPE REF TO cl_gui_alv_grid,
alv TYPE scrfname VALUE 'ALV',
w_custom_container TYPE REF TO cl_gui_custom_container.
PARAMETERS : p_column AS CHECKBOX,
p_line AS CHECKBOX,
p_cell AS CHECKBOX.
START-OF-SELECTION.
PERFORM get_data.
END-OF-SELECTION.
PERFORM fill_catalog.
PERFORM fill_layout.
CALL SCREEN 100.
*MODULE status_0100 OUTPUT.
*
SET PF-STATUS '100'.
*
*ENDMODULE. " status_2000 OUTPUT
MODULE user_command_0100 INPUT.
DATA : w_okcode LIKE sy-ucomm.
MOVE okcode TO w_okcode.
CLEAR okcode.
CASE w_okcode.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " user_command_2000 INPUT
----
MODULE alv_grid OUTPUT *
----
........ *
----
MODULE alv_grid OUTPUT.
IF w_custom_container IS INITIAL.
PERFORM create_objects.
PERFORM display_alv_grid.
ENDIF.
ENDMODULE. " alv_grid OUTPUT
----
FORM create_objects *
----
........ *
----
FORM create_objects.
CREATE OBJECT w_custom_container
EXPORTING
container_name = alv.
CREATE OBJECT w_alv_grid
EXPORTING
i_parent = w_custom_container.
ENDFORM. " create_objects
----
FORM display_alv_grid *
----
........ *
----
FORM display_alv_grid.
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. " display_alv_grid
----
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 8 TO wa_mara-value.
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. " get_data
----
FORM fill_catalog *
----
........ *
----
FORM fill_catalog.
DATA : w_position TYPE i VALUE '1',
w_row TYPE i VALUE '1'.
CLEAR wa_fieldcat.
MOVE w_row TO wa_fieldcat-row_pos.
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 'KUNNR' TO wa_fieldcat-fieldname.
MOVE 'KUNNR' TO wa_fieldcat-ref_table.
MOVE 'KUNNR' TO wa_fieldcat-ref_field.
CLEAR wa_fieldcat.
MOVE w_position TO wa_fieldcat-col_pos.
MOVE 'VALUE' TO wa_fieldcat-fieldname.
MOVE 'VALUE' TO wa_fieldcat-ref_table.
MOVE 'VALUE' 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.
w_position = 1.
ADD 1 TO w_row.
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. " fill_catalog
----
FORM fill_layout *
----
........ *
----
FORM fill_layout.
MOVE 'COLOR_LINE' TO is_layout-info_fname.
MOVE 'COLOR_CELL' TO is_layout-ctab_fname.
ENDFORM. " fill_layout
2007 Mar 02 9:08 AM
This is a sample code for coloring text and background as well as cells
first of all in layout give the name 'COL'(Capitals mandatory) this can be defined in the internal table as character of length 4.
because to define this color we define statement FS_SPFLI-COL = 'C610'.
in this one 'C' Means color and 6 color number and intensity(0 or 1) and inverse(0 or 1)....
after this modify that perticular colum with the modify statement.
(LAYOUT-INFO_FIELDNAME = 'COL'.
FS_SPFLI-COL = 'C610'.
MODIFY T_SPFLI INDEX W_I FROM Fs_SPFLI TRANSPORTING COL.)
Program start here
type-pools:
SLIS.
data: fs_fieldcat type slis_t_fieldcat_alv.
data: t_fieldcat like line of fs_fieldcat.
data: layout type SLIS_LAYOUT_ALV.
parameter : p_col type i.
parameters: W_I TYPE I.
LAYOUT-BOX_FIELDNAME = 'CHECK'.
LAYOUT-INFO_FIELDNAME = 'COL'.
LAYOUT-COLTAB_FIELDNAME = 'CELL'.
DATA: BEGIN OF FS_SPFLI,
COL(4),
CHECK,
CARRID TYPE SPFLI-CARRID,
CONNID TYPE SPFLI-CONNID,
COUNTRYFR TYPE SPFLI-COUNTRYFR,
CITYFROM TYPE SPFLI-CITYFROM,
AIRPFROM TYPE SPFLI-AIRPFROM,
COUNTRYTO TYPE SPFLI-COUNTRYTO,
CITYTO TYPE SPFLI-CITYTO,
DEPTIME TYPE SPFLI-DEPTIME,
ARRTIME TYPE SPFLI-ARRTIME,
DISTANCE TYPE SPFLI-DISTANCE,
FLTYPE TYPE SPFLI-FLTYPE,
PERIOD TYPE SPFLI-PERIOD,
CELL TYPE SLIS_t_SPECIALCOL_ALV,
END OF FS_SPFLI.
data: cell1 type slis_specialcol_alv.
data: t_spfli like
standard table
of fs_spfli .
select carrid
connid
countryfr
cityfrom
airpfrom
countryto
CITYTO
DEPTIME
ARRTIME
DISTANCE
FLTYPE
PERIOD
into corresponding fields of table t_spfli
from spfli.
FS_SPFLI-COL = 'C610'.
MODIFY T_SPFLI INDEX W_I FROM Fs_SPFLI TRANSPORTING COL.
CELL1-FIELDNAME = 'DISTANCE'.
cell1-COLOR-COL = '5'.
cell1-color-int = '1'.
append cell1 to fs_spfli-cell.
MODIFY t_SPFLI INDEX p_col FROM Fs_SPFLI TRANSPORTING CELL.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'CARRID'.
t_fieldcat-col_pos = 1.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-KEY = 'X'.
T_FIELDCAT-ICON = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'CONNID'.
t_fieldcat-col_pos = 2.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-KEY = 'X'.
T_FIELDCAT-ICON = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'COUNTRYFR'.
t_fieldcat-col_pos = 3.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'CITYFROM'.
t_fieldcat-col_pos = 4.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-ICON = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'AIRPFROM'.
t_fieldcat-col_pos = 5.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-ICON = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'CITYTO'.
t_fieldcat-col_pos = 6.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'DEPTIME'.
t_fieldcat-col_pos = 6.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'ARRTIME'.
t_fieldcat-col_pos = 7.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'DISTANCE'.
t_fieldcat-col_pos = 8.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-ICON = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'FLTYPE'.
t_fieldcat-col_pos = 9.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
t_FIELDCAT-REF_TABNAME = 'SPFLI'.
t_fieldcat-fieldname = 'PERIOD'.
t_fieldcat-col_pos = 10.
t_fieldcat-hotspot = 'X'.
T_FIELDCAT-ICON = 'X'.
T_FIELDCAT-EMPHASIZE = 'C611'.
append t_fieldcat to fs_fieldcat.
clear t_fieldcat.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_INTERFACE_CHECK = ' '
I_BYPASSING_BUFFER =
I_BUFFER_ACTIVE = ' '
I_CALLBACK_PROGRAM = ' '
I_CALLBACK_PF_STATUS_SET = ' '
I_CALLBACK_USER_COMMAND = ' '
I_STRUCTURE_NAME =
IS_LAYOUT = layout
IT_FIELDCAT = fs_fieldcat
IT_EXCLUDING =
IT_SPECIAL_GROUPS =
IT_SORT =
IT_FILTER =
IS_SEL_HIDE =
I_DEFAULT = 'X'
I_SAVE = ' '
IS_VARIANT =
IT_EVENTS =
IT_EVENT_EXIT =
IS_PRINT =
IS_REPREP_ID =
I_SCREEN_START_COLUMN = 0
I_SCREEN_START_LINE = 0
I_SCREEN_END_COLUMN = 0
I_SCREEN_END_LINE = 0
IR_SALV_LIST_ADAPTER =
IT_EXCEPT_QINFO =
I_SUPPRESS_EMPTY_DATA = ABAP_FALSE
IMPORTING
E_EXIT_CAUSED_BY_CALLER =
ES_EXIT_CAUSED_BY_USER =
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.
rewards its help u
Vijay Pawar