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 examples needed

Former Member
0 Kudos
186

Can any post me some alv grid examples,to find which row has been selected .

1 ACCEPTED SOLUTION

athavanraja
Active Contributor
0 Kudos
52

also look at the demo programs

BCALVGRID* in your sap system

Regards

Raja

4 REPLIES 4

athavanraja
Active Contributor
0 Kudos
53

also look at the demo programs

BCALVGRID* in your sap system

Regards

Raja

0 Kudos
52

Here is a little sample....



REPORT ZRICH_0006.

TABLES: MARA.

TYPE-POOLS: SLIS, ICON.

* Internal Tables
DATA: BEGIN OF IALV OCCURS 0,
      MATNR TYPE MARA-MATNR,
      MAKTX TYPE MAKT-MAKTX,
      END OF IALV .

* 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,
      ROW_TABLE TYPE LVC_T_ROW WITH HEADER LINE,
      OK_CODE LIKE SY-UCOMM,
      LAYOUT  TYPE LVC_S_LAYO,
      FIELDCAT TYPE LVC_T_FCAT.

SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION.

  PERFORM GET_DATA.

  IF IALV[] IS INITIAL.
    MESSAGE S429(MO).
    EXIT.
  ENDIF.

  CALL SCREEN 100.

************************************************************************
*      Module  status_0100  OUTPUT
************************************************************************
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS '0100'.
  SET TITLEBAR '0100'.
ENDMODULE.

************************************************************************
*      Module  ALV_0100  OUTPUT
************************************************************************
MODULE ALV_0100 OUTPUT.

  DATA: VARIANT TYPE  DISVARIANT.
  DATA: LT_EXCLUDE TYPE UI_FUNCTIONS.

  VARIANT-REPORT = SY-REPID.
  VARIANT-USERNAME = SY-UNAME.

* Create Controls
  CREATE OBJECT ALV_CONTAINER
         EXPORTING CONTAINER_NAME = 'ALV_CONTAINER'.

  CREATE OBJECT ALV_GRID
         EXPORTING  I_PARENT =  ALV_CONTAINER.

*  Populate Field Catalog
  PERFORM GET_FIELDCATALOG.

* Optionally restrict generic functions to 'change only'.
* (The user shall not be able to add new lines).
  PERFORM EXCLUDE_TB_FUNCTIONS CHANGING LT_EXCLUDE.

* Set selection mode to "D"  --  Multiple Lines
  LAYOUT-SEL_MODE = 'D'.

  CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
           IS_LAYOUT              = LAYOUT
           IT_TOOLBAR_EXCLUDING   = LT_EXCLUDE
           IS_VARIANT             = VARIANT
           I_SAVE                 = 'A'
           I_STRUCTURE_NAME       = 'IALV'
      CHANGING
           IT_OUTTAB       = IALV[]
           IT_FIELDCATALOG = FIELDCAT[].


ENDMODULE.

************************************************************************
*      Module  USER_COMMAND_0100  INPUT
************************************************************************
MODULE USER_COMMAND_0100 INPUT.

  CASE SY-UCOMM.

    WHEN 'BACK' OR 'CANC'.
      PERFORM FREE_CONTAINERS.
      IF SY-SUBRC = 0.
        SET SCREEN 0.
        LEAVE SCREEN.
      ELSE.
        LEAVE PROGRAM.
      ENDIF.

    WHEN 'EXIT'.
      PERFORM FREE_CONTAINERS.
      LEAVE PROGRAM.

    WHEN 'CONTINUE'.

* Retrieve selected rows from ALV grid
      PERFORM GET_SELECTED_ROWS.

* Do something with those selected rows here
      LOOP AT INDEX_ROWS INTO INDEX.

        READ TABLE IALV INDEX INDEX-INDEX.
        IF SY-SUBRC = 0.

* Insert code here

        ENDIF.
      ENDLOOP.



      PERFORM FREE_CONTAINERS.
      LEAVE TO SCREEN 100.

  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_SELECTED_ROWS
************************************************************************
FORM GET_SELECTED_ROWS.

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

ENDFORM.

************************************************************************
*      Form  FREE_CONTAINERS
************************************************************************
FORM FREE_CONTAINERS.

  IF NOT ALV_CONTAINER IS INITIAL.
    CALL METHOD ALV_CONTAINER->FREE.
    CLEAR: ALV_CONTAINER.
    FREE : ALV_CONTAINER.
  ENDIF.

ENDFORM.

************************************************************************
*      Form  Get_Fieldcatalog - Set Up Columns/Headers
************************************************************************
FORM GET_FIELDCATALOG.

  DATA: LS_FCAT TYPE LVC_S_FCAT.
  DATA: COLUMNNO(3) TYPE N VALUE '0'.
  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.

ENDFORM.

***********************************************************************
*      Form  EXCLUDE_TB_FUNCTIONS
***********************************************************************
FORM EXCLUDE_TB_FUNCTIONS CHANGING PT_EXCLUDE TYPE UI_FUNCTIONS.
* Only allow to change data not to create new entries (exclude
* generic functions).

  DATA LS_EXCLUDE TYPE UI_FUNC.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_MOVE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.

ENDFORM.

Regards,

Rich Heilman

Former Member
0 Kudos
52

if you are comfortable with the classical approach, some of the best examples are located on this site:

http://www.sapdevelopment.co.uk/reporting/alv/alvgrid.htm

Rajib

0 Kudos
52

If you need ALV Grid control then it is very good example

http://www.henrikfrank.dk/abapexamples/ABAP%20objects%20and%20controls/alvgrid_control.htm

See it!!!

Reg,

Arpit