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: 

Displaying a Particular cell in red color in ALV GRID

Former Member
0 Kudos

Dear all,

i am using the class interface CL_ALV_CHANGED_DATA_PROTOCOL.

in that i am calling the method GET_CELL_VALUE to get cell value.

i want to display a particular cell if its value is less than 0.

there are some more methods like MODIFY_CELL, DISPLAY_PROTOCOL

MODIFY_STYLE , CREATE_DISPLAY_PROFILE.

Which one can i use , how to use that method .

Regards,

Balaji

1 ACCEPTED SOLUTION

Former Member
0 Kudos

CHECK THIS CODE IT MIGHT HELP U.

FORM handle_data_changed USING ir_data_changed

TYPE REF TO cl_alv_changed_data_protocol.

DATA : ls_mod_cell TYPE lvc_s_modi ,

lv_value TYPE lvc_value .

SORT ir_data_changed->mt_mod_cells BY row_id .

LOOP AT ir_data_changed->mt_mod_cells

INTO ls_mod_cell

WHERE fieldname = 'SEATSMAX' .

CALL METHOD ir_data_changed->get_cell_value

EXPORTING i_row_id = ls_mod_cell-row_id

i_fieldname = 'CARRID'

IMPORTING e_value = lv_value .

IF lv_value = 'THY' AND ls_mod_cell-value > '500' .

CALL METHOD ir_data_changed->add_protocol_entry

EXPORTING

i_msgid = 'SU'

i_msgno = '000'

i_msgty = 'E'

i_msgv1 = 'This number can not exceed 500 for '

i_msgv2 = lv_value

i_msgv3 = 'The value is et to ''500'''

i_fieldname = ls_mod_cell-fieldname

i_row_id = ls_mod_cell-row_id .

CALL METHOD ir_data_changed->modify_cell

EXPORTING i_row_id = ls_mod_cell-row_id

i_fieldname = ls_mod_cell-fieldname

i_value = '500' .

ENDIF .

ENDLOOP .

ENDFORM. " handle_data_changed

4 REPLIES 4

Former Member
0 Kudos

CHECK THIS CODE IT MIGHT HELP U.

FORM handle_data_changed USING ir_data_changed

TYPE REF TO cl_alv_changed_data_protocol.

DATA : ls_mod_cell TYPE lvc_s_modi ,

lv_value TYPE lvc_value .

SORT ir_data_changed->mt_mod_cells BY row_id .

LOOP AT ir_data_changed->mt_mod_cells

INTO ls_mod_cell

WHERE fieldname = 'SEATSMAX' .

CALL METHOD ir_data_changed->get_cell_value

EXPORTING i_row_id = ls_mod_cell-row_id

i_fieldname = 'CARRID'

IMPORTING e_value = lv_value .

IF lv_value = 'THY' AND ls_mod_cell-value > '500' .

CALL METHOD ir_data_changed->add_protocol_entry

EXPORTING

i_msgid = 'SU'

i_msgno = '000'

i_msgty = 'E'

i_msgv1 = 'This number can not exceed 500 for '

i_msgv2 = lv_value

i_msgv3 = 'The value is et to ''500'''

i_fieldname = ls_mod_cell-fieldname

i_row_id = ls_mod_cell-row_id .

CALL METHOD ir_data_changed->modify_cell

EXPORTING i_row_id = ls_mod_cell-row_id

i_fieldname = ls_mod_cell-fieldname

i_value = '500' .

ENDIF .

ENDLOOP .

ENDFORM. " handle_data_changed

Former Member
0 Kudos

GO THROUGH THIS IT WILL HELP U SOLVE UR PROBLEM

The ALV Grid has events “data_changed” and “data_changed_finished”. The former method is triggered just after the change at an editable field is perceived. Here you can make checks for the input. And the second event is triggered after the change is committed.

You can select the way how the control perceives data changes by using the method “register_edit_event”. You have two choices:

i. After return key is pressed: To select this way, to the parameter “i_event_id” pass “cl_gui_alv_grid=>mc_evt_enter”.

ii. After the field is modified and the cursor is moved to another field:

For this, pass “cl_gui_alv_grid=>mc_evt_modifies” to the same parameter.

To make events controlling data changes be triggered, you must select either way by calling this method. Otherwise, these events will not be triggered.

To control field data changes, ALV Grid uses an instance of the class “CL_ALV_CHANGED_DATA_PROTOCOL” and passes this via the event “data_changed”. Using methods of this class, you can get and modify cell values and produce error messages. Here are some of those methods:

get_cell_value

Gets the cell value. You pass the address of the cell to the interface.

modify_cell

Modifies the cell value addressed via parameters.

add_protocol_entry

Add a log entry. You make use of standard message interface with message type, message id, etc…

protocol_is_visible

Make the error table visible or not.

refresh_protocol

Refreshing log entries.

With the reference of the instance, you can reach information about modifications. These useful attribute tables are:

MT_MOD_CELLS

Contains addresses of modified cells with “row_id”s and “fieldname”s.

MP_MOD_ROWS

Contains modified rows. Its type is generic.

MT_GOOD_CELLS

Contains cells having proper values

MT_DELETED_ROWS

Contains rows deleted from the list

MT_INSERTED_ROWS

Contains rows inserted to the list

Utilizing these methods and attributes you can check and give proper message and also modify the cell content.

Former Member
0 Kudos

Hi Balaji,

Run this code for cell , row , coloumn coloring by selecting the check boxes

REPORT zex34 .

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

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

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

  • Table

TABLES : mara.

  • Type

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.

  • 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,

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_2000 OUTPUT

&----


  • text

----


MODULE status_0100 OUTPUT.

SET PF-STATUS '100'.

ENDMODULE. " status_2000 OUTPUT

&----


*& Module user_command_2000 INPUT

&----


  • text

----


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

&----


  • text

----


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

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


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

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


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

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


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

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


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

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

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM fill_layout.

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

ENDFORM. " fill_layout

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

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