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

Hi Everyone,

I am new comer to ABAP world, new to alv grid screens.

My requirement is I have 2 alv grid screens which populates the data through performs. Both the forms uses internal table and structures.

If an object is selected in first grid, then its corresponding data must occur in the second grid.

I tried out using method get_selected_rows of the class cl_gui_alv_grid, but still doesn't work.

Any help will be of great help to me.

Thanks,

Prashanth.

Message was edited by: Prashant Kumar

9 REPLIES 9

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Try this sample program....

Select an entry in the first ALV grid and hit enter. The other data will show up in the other ALV Grid.



report zrich_0006.

tables: mara.

type-pools: slis.

* Internal Tables
data: begin of ialv occurs 0,
      matnr type mara-matnr,
      maktx type makt-maktx,
      end of ialv .

* Internal Tables
data: begin of ialv2 occurs 0,
      matnr type mard-matnr,
      werks type mard-werks,
      lgort type mard-lgort,
      end of ialv2 .

* Miscellanous Variables
data: index_rows type lvc_t_row,
      index like line of index_rows.


data: alv_container type ref to cl_gui_custom_container,
      alv_grid type ref to cl_gui_alv_grid,
      alv_container2 type ref to cl_gui_custom_container,
      alv_grid2 type ref to cl_gui_alv_grid,
      ok_code like sy-ucomm,
        layout  type lvc_s_layo,
      fieldcat type lvc_t_fcat,
      fieldcat2 type lvc_t_fcat.

select-options: s_matnr for mara-matnr.

start-of-selection.

  perform get_data.


  call screen 100.

************************************************************************
*      Module  status_0100  OUTPUT
************************************************************************
module status_0100 output.

  data: variant type  disvariant.
  data: lt_exclude type ui_functions.
  data: ls_fcat type lvc_s_fcat.

  set pf-status '0100'.
  set titlebar '0100'.

  if not alv_container is initial.
    call method alv_container->free.
    clear: alv_container.
    free : alv_container.
  endif.
  if not alv_container2 is initial.
    call method alv_container2->free.
    clear: alv_container2.
    free : alv_container2.
  endif.


***   Code for first ALV Grid

* Create Controls
  create object alv_container
         exporting container_name = 'ALV_CONTAINER'.

*   create Event Receiver
  create object alv_grid
         exporting  i_parent =  alv_container.


  clear fieldcat.  refresh: fieldcat.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Material Number'.
  ls_fcat-coltext    = 'Material Number'.
  ls_fcat-fieldname  = 'MATNR'.
  ls_fcat-ref_table  = 'IALV'.
  ls_fcat-outputlen  = '18'.
  ls_fcat-col_pos    = 1.
  append ls_fcat to fieldcat.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Material Description'.
  ls_fcat-coltext    = 'Material Description'.
  ls_fcat-fieldname  = 'MATKX'.
  ls_fcat-ref_table  = 'IALV'.
  ls_fcat-outputlen  = '40'.
  ls_fcat-col_pos    = 2.
  append ls_fcat to fieldcat.


* Set selection mode to "D"  --  Multiple Lines
  layout-sel_mode = 'D'.

  call method alv_grid->set_table_for_first_display
      exporting
            is_layout              = layout
      changing
           it_outtab       = ialv[]
           it_fieldcatalog = fieldcat[].



***   Code for second ALV Grid

* Create Controls
  create object alv_container2
         exporting container_name = 'ALV_CONTAINER2'.

*   create Event Receiver
  create object alv_grid2
         exporting  i_parent =  alv_container2.

  clear fieldcat.  refresh: fieldcat.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Material Number'.
  ls_fcat-coltext    = 'Material Number'.
  ls_fcat-fieldname  = 'MATNR'.
  ls_fcat-ref_table  = 'IALV2'.
  ls_fcat-outputlen  = '18'.
  append ls_fcat to fieldcat2.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Plant'.
  ls_fcat-coltext    = 'Plant'.
  ls_fcat-fieldname  = 'MATNR'.
  ls_fcat-ref_table  = 'IALV2'.
  ls_fcat-outputlen  = '4'.
  append ls_fcat to fieldcat2.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Store Loc'.
  ls_fcat-coltext    = 'Store Loc'.
  ls_fcat-fieldname  = 'LGORT'.
  ls_fcat-ref_table  = 'IALV2'.
  ls_fcat-outputlen  = '4'.
  append ls_fcat to fieldcat2.

  call method alv_grid2->set_table_for_first_display
       changing
           it_outtab       = ialv2[]
           it_fieldcatalog = fieldcat2[].


endmodule.

************************************************************************
*      Module  USER_COMMAND_0100  INPUT
************************************************************************
module user_command_0100 input.

  case sy-ucomm.

    when 'BACK' or 'CANC'.
      if not alv_container is initial.
        call method alv_container->free.
        clear: alv_container.
        free : alv_container.
      endif.
      if not alv_container2 is initial.
        call method alv_container2->free.
        clear: alv_container2.
        free : alv_container2.
      endif.
      if sy-subrc = 0.
        set screen 0.
        leave screen.
      else.
        leave program.
      endif.

    when 'ENTER'.

* Get Selected rows from alv grid
      clear index_rows.  refresh index_rows.
      call method alv_grid->get_selected_rows
               importing
                     et_index_rows = index_rows.

* Do something with those selected rows here
      loop at index_rows into index.

        read table ialv index index-index.
        if sy-subrc = 0.

          perform get_more_data.
          leave to screen 100.


        endif.
      endloop.

  endcase.

endmodule.


*********************************************************************
*       FORM GET_DATA.
*********************************************************************
form get_data.

  select mara~matnr makt~maktx
             into corresponding fields of table ialv
                 from mara
                      inner join makt
                         on mara~matnr = makt~matnr
                                where mara~matnr in s_matnr
                                  and makt~spras = sy-langu.

  sort ialv ascending by matnr.

endform.

*********************************************************************
*       FORM GET_MORE_DATA.
*********************************************************************
form get_more_data.

  select matnr werks lgort
             into corresponding fields of table ialv2
                 from mard
                           where matnr = ialv-matnr.

  sort ialv2 ascending by matnr.

endform.

Regards,

Rich Heilman

Former Member
0 Kudos

Hi Rich,

I tried this by looking it in your replied in the previous threads, but it not work.

After selecting a row in one grid, i have to populate its corresponding data in other grid. The data in the second grid must appear when a row in the first grid is selected.

My requirement is not like this must happen when i press the enter button.

Thanks,

Prashant.

0 Kudos

This will definitely work better for you then. This uses a hotspot on the MATNR field. When you click it, the other data is filled in the other ALV grid. Works nice.



   report zrich_0006.

   tables: mara.

   type-pools: slis.

* Internal Tables
   data: begin of ialv occurs 0,
         matnr type mara-matnr,
         maktx type makt-maktx,
         end of ialv .

* Internal Tables
   data: begin of ialv2 occurs 0,
         matnr type mard-matnr,
         werks type mard-werks,
         lgort type mard-lgort,
         end of ialv2 .




***********************************************************************
*       CLASS lcl_event_receiver DEFINITION      Handles Hotspot
***********************************************************************
   class lcl_event_receiver definition.
     public section.
       methods handle_hotspot_click
          for event hotspot_click of cl_gui_alv_grid
         importing e_row_id.
     private section.
   endclass.

***********************************************************************
*       CLASS lCL_EVENT_RECEIVER IMPLEMENTATION    Handles Hotspot
***********************************************************************
   class lcl_event_receiver implementation.
     method handle_hotspot_click.
       perform get_details using e_row_id-index.
     endmethod.
   endclass.

   data: alv_container type ref to cl_gui_custom_container,
         event_receiver type ref to lcl_event_receiver,
         alv_grid type ref to cl_gui_alv_grid,
         alv_container2 type ref to cl_gui_custom_container,
         alv_grid2 type ref to cl_gui_alv_grid,
         ok_code like sy-ucomm,
           layout  type lvc_s_layo,
         fieldcat type lvc_t_fcat,
         fieldcat2 type lvc_t_fcat.

   select-options: s_matnr for mara-matnr.

   start-of-selection.

     perform get_data.


     call screen 100.





************************************************************************
*      Module  status_0100  OUTPUT
************************************************************************
   module status_0100 output.

     data: variant type  disvariant.
     data: lt_exclude type ui_functions.
     data: ls_fcat type lvc_s_fcat.

     set pf-status '0100'.
     set titlebar '0100'.

     check alv_container is initial.

***   Code for first ALV Grid

* Create Controls
     create object alv_container
            exporting container_name = 'ALV_CONTAINER'.

*   create Event Receiver
     create object event_receiver.

     create object alv_grid
            exporting  i_parent =  alv_container.


     clear fieldcat.  refresh: fieldcat.

     clear: ls_fcat.
     ls_fcat-reptext    = 'Material Number'.
     ls_fcat-coltext    = 'Material Number'.
     ls_fcat-fieldname  = 'MATNR'.
     ls_fcat-ref_table  = 'IALV'.
     ls_fcat-hotspot    = 'X'.
     ls_fcat-outputlen  = '18'.
     ls_fcat-col_pos    = 1.
     append ls_fcat to fieldcat.

     clear: ls_fcat.
     ls_fcat-reptext    = 'Material Description'.
     ls_fcat-coltext    = 'Material Description'.
     ls_fcat-fieldname  = 'MATKX'.
     ls_fcat-ref_table  = 'IALV'.
     ls_fcat-outputlen  = '40'.
     ls_fcat-col_pos    = 2.
     append ls_fcat to fieldcat.

     call method alv_grid->set_table_for_first_display
         changing
              it_outtab       = ialv[]
              it_fieldcatalog = fieldcat[].

*   handler for ALV grid
     set handler event_receiver->handle_hotspot_click for alv_grid.



***   Code for second ALV Grid

* Create Controls
     create object alv_container2
            exporting container_name = 'ALV_CONTAINER2'.

*   create Event Receiver
     create object alv_grid2
            exporting  i_parent =  alv_container2.

     clear fieldcat.  refresh: fieldcat.

     clear: ls_fcat.
     ls_fcat-reptext    = 'Material Number'.
     ls_fcat-coltext    = 'Material Number'.
     ls_fcat-fieldname  = 'MATNR'.
     ls_fcat-ref_table  = 'IALV2'.
     ls_fcat-outputlen  = '18'.
     append ls_fcat to fieldcat2.

     clear: ls_fcat.
     ls_fcat-reptext    = 'Plant'.
     ls_fcat-coltext    = 'Plant'.
     ls_fcat-fieldname  = 'MATNR'.
     ls_fcat-ref_table  = 'IALV2'.
     ls_fcat-outputlen  = '4'.
     append ls_fcat to fieldcat2.

     clear: ls_fcat.
     ls_fcat-reptext    = 'Store Loc'.
     ls_fcat-coltext    = 'Store Loc'.
     ls_fcat-fieldname  = 'LGORT'.
     ls_fcat-ref_table  = 'IALV2'.
     ls_fcat-outputlen  = '4'.
     append ls_fcat to fieldcat2.

     call method alv_grid2->set_table_for_first_display
          changing
              it_outtab       = ialv2[]
              it_fieldcatalog = fieldcat2[].


   endmodule.

************************************************************************
*      Module  USER_COMMAND_0100  INPUT
************************************************************************
   module user_command_0100 input.

     case sy-ucomm.

       when 'BACK' or 'CANC'.
         if not alv_container is initial.
           call method alv_container->free.
           clear: alv_container.
           free : alv_container.
         endif.
         if not alv_container2 is initial.
           call method alv_container2->free.
           clear: alv_container2.
           free : alv_container2.
         endif.
         if sy-subrc = 0.
           set screen 0.
           leave screen.
         else.
           leave program.
         endif.

     endcase.

   endmodule.


*********************************************************************
*       FORM GET_DATA.
*********************************************************************
   form get_data.

     select mara~matnr makt~maktx
                into corresponding fields of table ialv
                    from mara
                         inner join makt
                            on mara~matnr = makt~matnr
                                   where mara~matnr in s_matnr
                                     and makt~spras = sy-langu.

     sort ialv ascending by matnr.

   endform.

*********************************************************************
*       FORM GET_MORE_DATA.
*********************************************************************
   form get_more_data.

     select matnr werks lgort
                into corresponding fields of table ialv2
                    from mard
                              where matnr = ialv-matnr.

     sort ialv2 ascending by matnr.

   endform.



************************************************************************
* GET_DETAILS
************************************************************************
   form get_details using index.

     read table ialv index index.
     if sy-subrc = 0.

       perform get_more_data.
       call method alv_grid2->refresh_table_display.

     endif.

   endform.

Please don't forget to award points for helpful answers. Thanks.

Regards,

Rich Heilman

Message was edited by: Rich Heilman

0 Kudos

Hi Rich,

Is there any method which handles for single click on the alv grid, I have to perform this on a single click selection not double click.

Thanks,

Prashanth.

0 Kudos

Hotspot is a single click, i initially starting writing it for double-click event, but thought that you might say that you wanted single-click. HotSpot should definitly be single-click. At least in my system it is working as single click.

Regards,

Rich Heilman

0 Kudos

Thanks a lot,

It worked very fine.

Thanks once again.

Regards,

Prashanth.

0 Kudos

Cool, please mark this post as solved if your problem is in fact solved. Thanks.

Regards,

Rich Heilman

0 Kudos

Hi Rich,

With your example it worked, but I am populating the data in both the grid through this forms as I had mentioned earlier.

In the first grid i populate data from this form

PERFORM list1

CHANGING itab1.

and in the second grid i populate data from this form

PERFORM list2

USING

list1_info

CHANGING

itab2

I executed the program as how u had given, but at this place I get an error.

<b>read table itab1 index index.</b>

internal table itab1 has no header line-explicit specification of an output area with 'into wa' or 'assigning <fs> is required.

But the forms which i am using has already been declared with fs and wa.

Any help will be of great use to me.

Thanks,

Prashanth.

0 Kudos

Either add the extension WITH HEADER LINE to your internal table declartion, or you need to use the INTO <WA> syntax.

READ TABLE ITAB1 INTO WA1 INDEX INDEX.

Regards,

Rich Heilman