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: 

Need simple ALV Grid prgm with interactive

Former Member
0 Kudos

Hi experts,

pls anyone provide some sample code for simple alv grid for the study purpose.

i want to knw how the Interactive alv works, how to display some header details like company name and address in the top of the grid.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi

check the standard programs starting with

BCALV.

in se38 give BCALV* and press F4.you will get no of demo alv programs.

4 REPLIES 4

Former Member
0 Kudos

Hi,

Goto SE38 and search BCALV* and F4

Simple ALV Program...



program test.
class lcl_event_receiver definition deferred.
*
*********


data: ok_code like sy-ucomm,
      g_max type i value 100,
      gt_sflight type table of sflight,
      g_repid like sy-repid,
      gs_print    type lvc_s_prnt,
      gs_layout   type lvc_s_layo,
      mycontainer type scrfname value 'BCALVC_EVENT1_CONT1',
* reference to custom container: neccessary to bind ALV Control
  custom_container type ref to cl_gui_custom_container,
      grid1  type ref to cl_gui_alv_grid,
      event_receiver type ref to lcl_event_receiver.


* § Step 1. Define a (local) class for event handling
****************************************************************
* LOCAL CLASSES: Definition
****************************************************************
*===============================================================
* class c_event_receiver: local class to handle print events...
*    - PRINT_TOP_OF_PAGE (page header)
*    - PRINT_END_OF_PAGE (page footer)
*    - PRINT_TOP_OF_LIST (list header)
*    - PRINT_END_OF_LIST (list footer)
*
* Definition:
* ~~~~~~~~~~~
class lcl_event_receiver definition.

  public section.
* § 2. Define a method for each print event you need.
    methods:
    handle_top_of_page
        for event print_top_of_page of cl_gui_alv_grid,

    handle_end_of_page
        for event print_end_of_page of cl_gui_alv_grid,

    handle_top_of_list
        for event print_top_of_list of cl_gui_alv_grid,

    handle_end_of_list
        for event print_end_of_list of cl_gui_alv_grid.

  private section.
    data: pagenum type i.

endclass.
*
* c_event_receiver (Definition)
*===============================================================


****************************************************************
* LOCAL CLASSES: Implementation
****************************************************************
*===============================================================
* class c_event_receiver (Implementation)
*
class lcl_event_receiver implementation.
*§ 3. Implement your event handler methods. Use WRITE to provide output.
  method handle_top_of_page.
    data: tablename(30) type c.
    perform get_tablename changing tablename.
    write: /,'Event: PRINT_TOP_OF_PAGE'(001),
             'Table: '(002),tablename.

  endmethod.                           "handle_top_of_page
*-------------------------------------------
  method handle_end_of_page.
    data: tablename(30) type c.

    perform get_tablename changing tablename.
    add 1 to pagenum.
    write: /,'Event: PRINT_END_OF_PAGE'(003),
             text-002,tablename,
             'Number of pages so far: '(004), pagenum.

  endmethod.                           "handle_end_of_page
*-------------------------------------------
  method handle_top_of_list.
    data: tablename(30) type c.
    clear pagenum.
    perform get_tablename changing tablename.
    write: /,'Event: PRINT_TOP_OF_LIST'(005),
             text-002,tablename.

  endmethod.                           "handle_top_of_list
*-------------------------------------------
  method handle_end_of_list.
    data: tablename(30) type c.
    perform get_tablename changing tablename.
    write: /,'Event: PRINT_END_OF_LIST'(006),
             text-002,tablename.

  endmethod.                           "handle_end_of_list
*-------------------------------------------
endclass.
*
* c_event_receiver (Implementation)
*===================================================================

start-of-selection.
  select * from sflight into table gt_sflight up to g_max rows.
*
end-of-selection.
  g_repid = sy-repid.
  call screen 100.

*---------------------------------------------------------------------*
*       MODULE PBO OUTPUT                                             *
*---------------------------------------------------------------------*
module pbo output.
  set pf-status 'MAIN100'.
  set titlebar 'MAIN100'.
  if custom_container is initial.
* create a custom container control for our ALV Control
    create object custom_container
        exporting
            container_name = mycontainer
        exceptions
            cntl_error = 1
            cntl_system_error = 2
            create_error = 3
            lifetime_error = 4
            lifetime_dynpro_dynpro_link = 5.
    if sy-subrc ne 0.
* add your handling, for example
      call function 'POPUP_TO_INFORM'
           exporting
                titel = g_repid
                txt2  = sy-subrc
                txt1  = 'The control could not be created'(010).
    endif.
* create an instance of alv control
    create object grid1
          exporting i_parent = custom_container.
*
* Set a titlebar for the grid control
*
    gs_layout-grid_title = 'Flights'(100).

* § 5. In case of PRINT_END_OF_PAGE, you must set 'reservelns' to
*      the number of reserved lines at the end of a page.
*
* reserve two lines for the PRINT_END_OF_PAGE event
*
    gs_print-reservelns = 2.


********
* ->Create Object to receive events and link them to handler methods.
* When the ALV Control raises the event for the specified instance
* the corresponding method is automatically called.
*

********

* § 4. Link used print events and event handler methods.
    create object event_receiver.
    set handler event_receiver->handle_top_of_list for grid1.
    set handler event_receiver->handle_top_of_page for grid1.
    set handler event_receiver->handle_end_of_list for grid1.
    set handler event_receiver->handle_end_of_page for grid1.
*
  call method grid1->set_table_for_first_display
         exporting i_structure_name = 'SFLIGHT'
                   is_print         = gs_print
                   is_layout        = gs_layout
         changing  it_outtab        = gt_sflight.

  endif.
* Controls are not integrated into the TAB-Order
* Call "set_focus" if you want to make sure that 'the cursor'
* is active in your control.
  call method cl_gui_control=>set_focus exporting control = grid1.

* Control Framework flushes at the end of PBO automatically!
endmodule.
*---------------------------------------------------------------------*
*       MODULE PAI INPUT                                              *
*---------------------------------------------------------------------*
module pai input.
  case ok_code.
    when 'EXIT'.
      perform exit_program.
  endcase.
  clear ok_code.
endmodule.
*---------------------------------------------------------------------*
*       FORM EXIT_PROGRAM                                             *
*---------------------------------------------------------------------*
form exit_program.
  call method custom_container->free.
  call method cl_gui_cfw=>flush.
  if sy-subrc ne 0.
* add your handling, for example
    call function 'POPUP_TO_INFORM'
         exporting
              titel = g_repid
              txt2  = sy-subrc
              txt1  = 'Error in Flush'(009).
  endif.
  leave program.
endform.
*&---------------------------------------------------------------------*
*&      Form  GET_TABLENAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_TABLENAME  text
*----------------------------------------------------------------------*
form get_tablename changing p_tablename.
  data: lt_fieldcat type standard table of lvc_s_fcat,
        ls_fieldcat type lvc_s_fcat.

  call method grid1->get_frontend_fieldcatalog
                importing et_fieldcatalog = lt_fieldcat.


  call method cl_gui_cfw=>flush.
  if sy-subrc <> 0.
    p_tablename = 'No tablename in fieldcatalog!'(008).
    call function 'POPUP_TO_INFORM'
         exporting
              titel = g_repid
              txt2  = p_tablename
              txt1  = 'Error in Flush'(011).
  else.
    read table lt_fieldcat index 1 into ls_fieldcat.
    p_tablename = ls_fieldcat-ref_table.
  endif.

endform.                               " GET_TABLENAME

Former Member
0 Kudos

hi,

check this programme..

Display a Secondary List using ALV Grid

To display a secondary list when you click on one of the row items in an alv grid. The secondary list should also be an alv.

Try out this code. You will have to make a structure ZSTR same as the output internal table.

REPORT ZTEST_REP1 .

TABLES : MARA,

BHDGD,

zstr.

TYPES: BEGIN OF T_MARA,

MATNR LIKE MARA-MATNR,

ERNAM LIKE MARA-ERNAM,

END OF T_MARA.

CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.

*Constants for ALV Implementation

CONSTANTS: C_SET VALUE 'X',

C_RESET VALUE '0',

C_SAVE VALUE 'A',

C_EXIT(4) VALUE 'EXIT',

C_BACK(4) VALUE 'BACK',

C_CANC(4) VALUE 'CANC',

C_PGTOP(5) VALUE 'PGTOP',

C_PGUP(4) VALUE 'PGUP',

C_PGDN(4) VALUE 'PGDN',

C_PGEND(5) VALUE 'PGEND'.

DATA : I_MARA TYPE STANDARD TABLE OF T_MARA WITH HEADER LINE,

  • Internal table for fields catalouge

I_FIELDCAT TYPE LVC_T_FCAT WITH HEADER LINE,

  • i_fieldcat2 type lvc_t_fcat with header line,

  • Internal table for cursor position

I_GT_SELROWS TYPE LVC_T_ROW .

DATA : WA_MARA LIKE I_MARA,

WA_GRIDROW LIKE LVC_S_ROW,

WA_GRIDCOL LIKE LVC_S_COL.

*Data for ALV Implementation.

DATA: OK_CODE LIKE SY-UCOMM,

W_OK_CODE LIKE SY-UCOMM,

W_CALL TYPE I VALUE 1,

W_TAB LIKE SY-UCOMM VALUE 'TAB1',

W_SAVE, "For Parameter I_SAVE

W_VARIANT TYPE DISVARIANT, "For parameter IS_VARIANT

W_GRID TYPE REF TO CL_GUI_ALV_GRID,

  • w_grid1 type ref to cl_gui_alv_grid,

W_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

  • w_container1 type ref to cl_gui_custom_container,

W_REPID LIKE SY-REPID,

W_GS_PRINT TYPE LVC_S_PRNT,

W_GS_LAYOUT TYPE LVC_S_LAYO,

W_EVENT_REC TYPE REF TO LCL_EVENT_RECEIVER,

W_CONT_MAIN TYPE SCRFNAME VALUE 'CCCONTAINER',

W_LN TYPE I, "line number

W_INDEX LIKE SY-TABIX,

W_FLAG,

W_TEMP_VAL TYPE I.

----


  • Definition:

----


CLASS LCL_EVENT_RECEIVER DEFINITION.

PUBLIC SECTION.

METHODS:

HANDLE_TOP_OF_PAGE

FOR EVENT PRINT_TOP_OF_PAGE OF CL_GUI_ALV_GRID,

HANDLE_DOUBLE_CLICK

FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID

IMPORTING E_ROW E_COLUMN.

ENDCLASS.

----


  • CLASS LCL_EVENT_RECEIVER IMPLEMENTATION

----


CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

METHOD HANDLE_TOP_OF_PAGE.

PERFORM F_GET_HEADER.

ENDMETHOD. "handle_top_of_page

METHOD HANDLE_DOUBLE_CLICK.

  • The event DOUBLE_CLICK provides parameters for row and column

  • of the click. We use row parameter to select a line of the

  • corresponding internal table.

  • read selected row from internal table

READ TABLE I_MARA INDEX E_ROW-INDEX INTO WA_MARA.

IF SY-SUBRC <> 0.

  • message i001. " Cursor position not correct.

ELSE.

  • call dialog screen and display the details

call screen 200 starting at 10 5.

ENDIF.

ENDMETHOD. "handle_double_click

ENDCLASS.

*----


  • start-of-selection.

*----


START-OF-SELECTION.

SELECT MATNR ERNAM FROM MARA INTO TABLE I_MARA.

*----


  • End-of-Selection.

*----


END-OF-SELECTION.

  • Start of ALV part.

W_REPID = SY-REPID.

W_VARIANT-REPORT = W_REPID.

W_SAVE = C_SAVE.

W_CONT_MAIN = W_CONT_MAIN.

W_GS_LAYOUT = W_GS_LAYOUT.

W_GS_PRINT = W_GS_PRINT.

I_FIELDCAT = I_FIELDCAT.

CALL SCREEN 100.

&----


*& Form f_get_header

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM F_GET_HEADER.

DATA: L_LINE1 LIKE BHDGD-LINE1,

L_LINE2 LIKE BHDGD-LINE2.

CONSTANTS LC_SPACE VALUE ' '.

DATA: L_F1(7), L_F2(11), L_F3(9), L_F4(6), L_F5(11), L_F6(4), L_F7(8),

L_F8(4),L_F9(10), L_F11(11), L_F12(24), L_F13(4),

L_F14(3).

  • take the values of line1 and line2 into two new variables, otherwise

  • after coming back to the first screen from the print preview, the

  • header shows the condensed lines

L_LINE1 = BHDGD-LINE1.

L_LINE2 = BHDGD-LINE2.

CONDENSE L_LINE1.

CONDENSE L_LINE2.

*split the lines to display the whole lines within the

*stipulated report-width

SPLIT L_LINE1 AT LC_SPACE INTO L_F1 L_F2 L_F3 L_F4 L_F5 L_F6 L_F7 L_F8

L_F9 .

SPLIT L_LINE2 AT LC_SPACE INTO L_F11 L_F12 L_F13 L_F14.

L_F14 = SY-PAGNO.

WRITE:/1 L_F1, 9 L_F2, 40 L_F3, 50 L_F4, 57 L_F5, 88 L_F6, 93 L_F7 ,

103 L_F8 , 108 L_F9 .

WRITE:/1 L_F11, 40 TEXT-012, 78 L_F12, 103 L_F13, 108 L_F14.

ENDFORM. " f_get_header

&----


*& Module STATUS_0100 OUTPUT

&----


  • text

----


MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'STAT'.

SET TITLEBAR 'TITL'.

ENDMODULE. " STATUS_0100 OUTPUT

&----


*& Module USER_COMMAND_0100 INPUT

&----


  • text

----


MODULE USER_COMMAND_0100 INPUT.

CASE SY-UCOMM .

WHEN C_EXIT OR C_BACK OR C_CANC.

IF NOT W_CONTAINER IS INITIAL.

CALL METHOD W_CONTAINER->FREE.

ENDIF.

LEAVE TO SCREEN 0.

WHEN C_PGTOP.

WA_GRIDROW-INDEX = 1.

WHEN C_PGUP.

IF WA_GRIDROW-INDEX <= 15.

WA_GRIDROW-INDEX = 1.

ELSE.

WA_GRIDROW-INDEX = WA_GRIDROW-INDEX - 15.

ENDIF.

WHEN C_PGDN.

PERFORM F_GET_NO_ROWS.

W_TEMP_VAL = W_LN - WA_GRIDROW-INDEX.

IF W_TEMP_VAL < 15.

WA_GRIDROW-INDEX = W_LN.

ELSE.

WA_GRIDROW-INDEX = WA_GRIDROW-INDEX + 15.

ENDIF.

WHEN C_PGEND.

PERFORM F_GET_NO_ROWS.

WA_GRIDROW-INDEX = W_LN.

ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

&----


*& Form f_get_no_rows

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM F_GET_NO_ROWS.

DESCRIBE TABLE I_MARA LINES W_LN.

ENDFORM. " f_get_no_rows

&----


*& Module DISPLAY_0100 OUTPUT

&----


  • text

----


MODULE DISPLAY_0100 OUTPUT.

IF NOT WA_GRIDROW IS INITIAL

AND NOT WA_GRIDCOL IS INITIAL.

CALL METHOD W_GRID->SET_SCROLL_INFO_VIA_ID

EXPORTING

IS_ROW_INFO = WA_GRIDROW

IS_COL_INFO = WA_GRIDCOL .

CALL METHOD W_GRID->SET_CURRENT_CELL_VIA_ID

EXPORTING

IS_ROW_ID = WA_GRIDROW

IS_COLUMN_ID = WA_GRIDCOL .

ENDIF.

CALL METHOD W_GRID->GET_SCROLL_INFO_VIA_ID

IMPORTING

ES_ROW_INFO = WA_GRIDROW

ES_COL_INFO = WA_GRIDCOL .

CALL METHOD W_GRID->GET_SELECTED_ROWS

IMPORTING

ET_INDEX_ROWS = I_GT_SELROWS[].

  • Build the fieldcat according to structure

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = 'ZSTR'

CHANGING

CT_FIELDCAT = I_FIELDCAT[].

LOOP AT I_FIELDCAT.

W_INDEX = SY-TABIX.

CASE I_FIELDCAT-FIELDNAME.

WHEN 'MATNR'.

I_FIELDCAT-SCRTEXT_S = 'MATNR'.

I_FIELDCAT-KEY = ' '.

I_FIELDCAT-COL_POS = '1'.

WHEN 'ERNAM'.

I_FIELDCAT-SCRTEXT_S = 'ERDAT'.

I_FIELDCAT-OUTPUTLEN = '18'.

I_FIELDCAT-COL_POS = '2'.

ENDCASE.

MODIFY I_FIELDCAT INDEX W_INDEX.

ENDLOOP.

READ TABLE I_FIELDCAT INDEX 1 .

IF W_CALL = 1.

PERFORM F_STD_HEADER.

CALL METHOD W_GRID->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING

IS_VARIANT = W_VARIANT

I_SAVE = W_SAVE

CHANGING

IT_OUTTAB = I_MARA[]

IT_FIELDCATALOG = I_FIELDCAT[]

EXCEPTIONS

INVALID_PARAMETER_COMBINATION = 1

PROGRAM_ERROR = 2

OTHERS = 3.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

EXIT.

ENDIF.

CREATE OBJECT W_EVENT_REC.

SET HANDLER W_EVENT_REC->HANDLE_TOP_OF_PAGE FOR W_GRID.

CREATE OBJECT W_EVENT_REC.

SET HANDLER W_EVENT_REC->HANDLE_DOUBLE_CLICK FOR W_GRID.

W_FLAG = C_RESET.

CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = W_GRID.

W_CALL = 0.

ENDIF.

ENDMODULE. " DISPLAY_0100 OUTPUT

&----


*& Form f_std_header

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM F_STD_HEADER.

ENDFORM. " f_std_header

&----


*& Module DYNPRONR_CHECK_500 OUTPUT

&----


  • text

----


MODULE DYNPRONR_CHECK_500 OUTPUT.

  • if w_dynpronr is initial.

  • w_dynpronr = '0100'.

  • endif.

ENDMODULE. " DYNPRONR_CHECK_500 OUTPUT

&----


*& Module create_objects_0100 OUTPUT

&----


  • text

----


MODULE create_objects_0100 OUTPUT.

check w_container is initial .

create object w_container

exporting

container_name = 'CC'.

create object w_grid

exporting

i_parent = w_container.

w_flag = c_set.

w_flag = w_flag.

ENDMODULE. " create_objects_0100 OUTPUT

&----


*& Module STATUS_0200 OUTPUT

&----


  • text

----


MODULE STATUS_0200 OUTPUT.

SET PF-STATUS 'ST20'.

SET TITLEBAR '200'.

zstr-matnr = wa_mara-matnr.

zstr-ernam = wa_mara-ernam.

ENDMODULE. " STATUS_0200 OUTPUT

&----


*& Module USER_COMMAND_0200 INPUT

&----


  • text

----


MODULE USER_COMMAND_0200 INPUT.

move ok_code to w_ok_code.

clear ok_code.

case w_ok_code.

when c_back or c_exit or c_canc.

leave to screen 0.

endcase.

clear w_ok_code.

ENDMODULE. " USER_COMMAND_0200 INPUT

*-- End of Program

Regards,

Deepthi.

Former Member
0 Kudos

Hi, see standard reports BCALV_GRID_0*

Former Member
0 Kudos

hi

check the standard programs starting with

BCALV.

in se38 give BCALV* and press F4.you will get no of demo alv programs.