Skip to Content
author's profile photo Former Member
Former Member

On double click in a grid change the color of the row

Hi everyone,

In my alv grid, when i double click on a row the row color must change to light red and also perform some task.

How can I achieve this?

Any help from anyone of you will be of great help

thanks in advance.

PK.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Posted on Oct 07, 2005 at 07:02 PM

    Implement the following sample program. It will show you how to color a line, column or cell of alv grid. Check the appropriate check box and click execute.

    
    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.
    
    

    Regards,

    RIch Heilman

    Add a comment
    10|10000 characters needed characters exceeded

    • Here is a modified version which includes coloring of the line on double click of the line.

      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.
      
      
      ***********************************************************************
      *       CLASS cl_event_receiver DEFINITION
      ***********************************************************************
      class cl_event_receiver definition.
        public section.
          methods handle_double_click
            for event double_click of cl_gui_alv_grid
            importing e_row e_column.
        private section.
      endclass.
      
      ***********************************************************************
      *       CLASS CL_EVENT_RECEIVER IMPLEMENTATION
      ***********************************************************************
      class cl_event_receiver implementation.
        method handle_double_click.
          perform ondoubleclick using e_row-index.
        endmethod.
      endclass.
      
      * 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,
             event_receiver type ref to cl_event_receiver.
      
      
      parameters : p_column as checkbox,
                   p_line   as checkbox,
                   p_cell   as checkbox.
      
      initialization.
      
        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.
      
      create object event_receiver.
      
      * 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.
      
      *   handler for ALV grid
        set handler event_receiver->handle_double_click for w_alv_grid.
      
      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.
          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.
        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.
      
      ************************************************************************
      * ONDoubleCLick
      ************************************************************************
      form ondoubleclick using index.
      
        read table it_mara into wa_mara index index.
        if sy-subrc = 0.
         move 'C410' to wa_mara-color_line.
         modify it_mara from wa_mara index index.
        endif.
      
        call method w_alv_grid->REFRESH_TABLE_DISPLAY.
      
      endform.
      
      

      Please make sure to award points for helpful answers. Thanks.

      Regards,

      Rich Heilman

  • Posted on Oct 07, 2005 at 07:08 PM

    Are you using the function module or the OO methods for your ALV grid display? You need to capture your double click using the event handle_double_click of the class cl_gui_alv_grid if you are using OO or give USER_COMMAND routine to the function module call and handle the double click okcode in your FORM. Now as far as the color is concerned, you can change it by setting the attributes of the layout and then doing a refresh of the ALV grid. Read the documentation, it has all the details.

    Srinivas

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.