ALV - CALL METHOD GO_GRID->GET_SELECTED_ROWS

Question: Hi ABAPers,

I am trying to write a program to let my user to change the data in one of the customized table.

1. Prog will run by displaying all data from the table in ALV format.
2. User can select to change, delete or add data to this customized table.

Below show partly of my program.

Here is my question:
1. The prog able to display all data in ALV for the 1st time. No selection done on 1st time. Then i select one line item form the ALV list. Press "change". Progr will bring me to another screen and change some data. Then "save" it and return back to the ALV list display screen.
Screen will display all data inclde the changed line item.
QUESTION: When i select another line item from the ALV screen again, the system will prompt no line item selected. It seem like the code:
* Read index of selected rows
CALL METHOD GO_GRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = GI_INDEX_ROWS.
NOT work. CAN ANYONE TELL ME WHERE I DID WRONG??????


REPORT ZTEST.

TYPE-POOLS: ICON.

TABLES: ZTEST.

CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.

*--------------------------------------------------------------------
* G L O B A L I N T E R N A L T A B L E S
*--------------------------------------------------------------------
TYPES: BEGIN OF ST_ZTEST.
INCLUDE STRUCTURE ZTEST.
TYPES: END OF ST_ZTEST.

TYPES: TT_ZTEST TYPE STANDARD TABLE OF ST_ZTEST.

DATA: GI_ZTEST TYPE TT_ZTEST.

*--------------------------------------------------------------------
* G L O B A L D A T A
*--------------------------------------------------------------------
DATA: OK_CODE LIKE SY-UCOMM,
G_WA_ZTEST TYPE ST_ZTEST,
GS_LAYOUT TYPE LVC_S_LAYO.

* Declare reference variables to the ALV grid and the container
DATA: GO_GRID TYPE REF TO CL_GUI_ALV_GRID,
GO_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
O_EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.

* Work area for screen 200
DATA: G_SCREEN200 LIKE ZTEST.

* Data for storing information about selected rows in the grid
DATA:
* Internal table
GI_INDEX_ROWS TYPE LVC_T_ROW,
* Information about 1 row
G_SELECTED_ROW LIKE LVC_S_ROW.

*DATA: G_SCREEN400 LIKE ZTEST.

*--------------------------------------------------------------------
* C L A S S E S
*--------------------------------------------------------------------
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_TOOLBAR FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING
E_OBJECT E_INTERACTIVE,

HANDLE_USER_COMMAND FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
ENDCLASS.

*---------------------------------------------------------------------*
* CLASS lcl_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

* Event handler method for event toolbar.
METHOD HANDLE_TOOLBAR.

* Constants for button type
CONSTANTS:
C_BUTTON_NORMAL TYPE I VALUE 0,
C_MENU_AND_DEFAULT_BUTTON TYPE I VALUE 1,
C_MENU TYPE I VALUE 2,
C_SEPARATOR TYPE I VALUE 3,
C_RADIO_BUTTON TYPE I VALUE 4,
C_CHECKBOX TYPE I VALUE 5,
C_MENU_ENTRY TYPE I VALUE 6.

DATA: LS_TOOLBAR TYPE STB_BUTTON.

* Append seperator to the normal toolbar
CLEAR LS_TOOLBAR.
MOVE C_SEPARATOR TO LS_TOOLBAR-BUTN_TYPE.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.

* Append a new button that to the toolbar. Use E_OBJECT of
* event toolbar. E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.
* This class has one attribute MT_TOOLBAR which is of table type
* TTB_BUTTON. The structure is STB_BUTTON
CLEAR LS_TOOLBAR.

MOVE 'INSERT' TO LS_TOOLBAR-FUNCTION.
MOVE ICON_INSERT_ROW TO LS_TOOLBAR-ICON.
MOVE 'INSERT RECORD' TO LS_TOOLBAR-QUICKINFO.
MOVE 'Insert' TO LS_TOOLBAR-TEXT.
MOVE ' ' TO LS_TOOLBAR-DISABLED.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.

MOVE 'CHANGE' TO LS_TOOLBAR-FUNCTION.
MOVE ICON_CHANGE TO LS_TOOLBAR-ICON.
MOVE 'Change ZTEST' TO LS_TOOLBAR-QUICKINFO.
MOVE 'Change' TO LS_TOOLBAR-TEXT.
MOVE ' ' TO LS_TOOLBAR-DISABLED.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.

MOVE 'DELETE' TO LS_TOOLBAR-FUNCTION.
MOVE ICON_DELETE TO LS_TOOLBAR-ICON.
MOVE 'DELETE RECORD' TO LS_TOOLBAR-QUICKINFO.
MOVE 'Delete' TO LS_TOOLBAR-TEXT.
MOVE ' ' TO LS_TOOLBAR-DISABLED.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.

ENDMETHOD.

METHOD HANDLE_USER_COMMAND.
* Handle own functions defined in the toolbar
CASE E_UCOMM.
WHEN 'CHANGE'.
PERFORM CHANGE_ZTEST.

WHEN 'DELETE'.
* PERFORM DELETE_ZTEST.

WHEN 'INSERT'.
* PERFORM INSERT_ZTEST.

ENDCASE.

ENDMETHOD.

ENDCLASS.

*--------------------------------------------------------------------
* S T A R T - O F - S E L E C T I O N.
*--------------------------------------------------------------------
START-OF-SELECTION.
SET SCREEN '100'.

*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT

*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'STATUS_0100'.
* SET TITLEBAR 'xxx'.

DATA: L_LAYOUT TYPE DISVARIANT,
L_LINES TYPE I.

* After returning from screen 200 the line that was selected before
* going to screen 200, should be selected again. The table gi_index_rows
* was the output table from the GET_SELECTED_ROWS method in form
* change_ZTEST

DESCRIBE TABLE GI_INDEX_ROWS LINES L_LINES.
IF L_LINES > 0.
CALL METHOD GO_GRID->SET_SELECTED_ROWS
EXPORTING
IT_INDEX_ROWS = GI_INDEX_ROWS.

CALL METHOD CL_GUI_CFW=>FLUSH.
* REFRESH GI_INDEX_ROWS.
ENDIF.

PERFORM GET_DATA.

* Create objects for container and ALV grid
CREATE OBJECT GO_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = 'ALV_CONTAINER'.

CREATE OBJECT GO_GRID
EXPORTING
I_PARENT = GO_CUSTOM_CONTAINER.

* Create object for event_receiver class
* and set handlers
CREATE OBJECT O_EVENT_RECEIVER.
SET HANDLER O_EVENT_RECEIVER->HANDLE_USER_COMMAND FOR GO_GRID.
SET HANDLER O_EVENT_RECEIVER->HANDLE_TOOLBAR FOR GO_GRID.

* Layout (Variant) for ALV grid
L_LAYOUT-REPORT = SY-REPID. "Layout fo report

*---------------------------------------------------------------
* Setup the grid layout using a variable of structure lvc_s_layo
*---------------------------------------------------------------
* Set grid title
GS_LAYOUT-GRID_TITLE = 'ZTEST'.

LOOP AT GI_ZTEST INTO G_WA_ZTEST.
MODIFY GI_ZTEST FROM G_WA_ZTEST.
ENDLOOP.
* Grid setup for first display
CALL METHOD GO_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME = 'ZTEST'
IS_VARIANT = L_LAYOUT
I_SAVE = 'A'
IS_LAYOUT = GS_LAYOUT
CHANGING IT_OUTTAB = GI_ZTEST.

*-- End of grid setup -------------------------------------------

* Raise event toolbar to show the modified toolbar
CALL METHOD GO_GRID->SET_TOOLBAR_INTERACTIVE.

* Set focus to the grid. This is not necessary in this
* example as there is only one control on the screen
CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = GO_GRID.

ENDMODULE. " STATUS_0100 OUTPUT

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT

*&---------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE OK_CODE.
WHEN 'EXIT100'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT

*&---------------------------------------------------------------------
*& Module STATUS_0200 OUTPUT
*&---------------------------------------------------------------------
* text
*----------------------------------------------------------------------
MODULE STATUS_0200 OUTPUT.
SET PF-STATUS 'STATUS_0200'.
* SET TITLEBAR 'xxx'.
ENDMODULE. " USER_COMMAND_0200 OUTPUT

*&---------------------------------------------------------------------
*& Module USER_COMMAND_0200 INPUT
*&---------------------------------------------------------------------
* text
*---------------------------------------------------------------------
MODULE USER_COMMAND_0200 INPUT.
CASE OK_CODE.
WHEN 'EXIT200'.
LEAVE TO SCREEN 0100.
WHEN'SAVE'.
PERFORM SAVE_CHANGES.
ENDCASE.
ENDMODULE. " USER_COMMAND_0200 INPUT

*&---------------------------------------------------------------------*
*& Form get_data

*&---------------------------------------------------------------------*
* text

*----------------------------------------------------------------------*
FORM GET_DATA.
* Read data from table SFLIGHT
SELECT *
FROM ZTEST
INTO TABLE GI_ZTEST.
ENDFORM. " load_data_into_grid

*&---------------------------------------------------------------------

*& Form change_ZTEST

*&---------------------------------------------------------------------

* Reads the contents of the selected row in the grid, ans transfers
* the data to screen 200, where it can be changed and saved.
*----------------------------------------------------------------------

FORM CHANGE_ZTEST.

REFRESH GI_INDEX_ROWS.
CLEAR G_SELECTED_ROW.

* Read index of selected rows
CALL METHOD GO_GRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = GI_INDEX_ROWS.
* Check if any row are selected at all. If not
* table gi_index_rows will be empty
DESCRIBE TABLE GI_INDEX_ROWS LINES L_LINES.
IF L_LINES = 0.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
TEXTLINE1 = 'You must choose a line'.
EXIT.
ENDIF.

LOOP AT GI_INDEX_ROWS INTO G_SELECTED_ROW.
IF SY-TABIX = 1.
READ TABLE GI_ZTEST INDEX G_SELECTED_ROW-INDEX INTO G_WA_ZTEST.
ENDIF.
ENDLOOP.

* Transfer data from the selected row to screenm 200 and show
* screen 200
CLEAR G_SCREEN200.
MOVE-CORRESPONDING G_WA_ZTEST TO G_SCREEN200.
LEAVE TO SCREEN '200'.

ENDFORM. " change_flight

*&---------------------------------------------------------------------
*& Form save_changes
*&---------------------------------------------------------------------
* Changes made in screen 200 are written to the datbase table
* ZTEST, and to the grid table gi_ZTEST, and the grid is
* updated with method refresh_table_display to display the changes
*----------------------------------------------------------------------

FORM SAVE_CHANGES.

* Update database table
MODIFY ZTEST FROM G_SCREEN200.

MOVE-CORRESPONDING G_SCREEN200 TO G_WA_ZTEST.

MODIFY GI_ZTEST INDEX G_SELECTED_ROW-INDEX FROM G_WA_ZTEST.

* Refresh grid
CALL METHOD GO_GRID->REFRESH_TABLE_DISPLAY.
* CALL METHOD CL_GUI_CFW=>FLUSH.

LEAVE TO SCREEN '0100'.
ENDFORM. " save_changes
_________________
Best regards,
SAP - IT - MAN

Answer:
Hi sapitman, your problem is in the declaration. I´ve used this method several times in different programs. You have to do the following:

DATA: l_lines type i.
DATA: lt_row TYPE lvc_t_row.
DATA: ls_row TYPE lvc_s_row.

DATA: gt_ztest TYPE TABLE OF ztest.
DATA: gs_ztest LIKE LINE OF gt_ztest.

DATA: gt_ztest_selected TYPE TABLE OF ztest.
DATA: gs_ztest_selected LIKE LINE OF gt_ztest.

* Select the info you want to show on the Grid.
SELECT * FROM ztest
INTO CORRESPONDING FIELDS OF TABLE gt_ztest.

CALL METHOD GO_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME = 'ZTEST'
IS_VARIANT = L_LAYOUT
I_SAVE = 'A'
IS_LAYOUT = GS_LAYOUT
CHANGING IT_OUTTAB = gt_ztest. "Attention!!
....
....
....

* Clear the structures.
CLEAR lt_row. "Table of rows selected
CLEAR ls_row. "Header of the table of rows

* Calling the method.
CALL METHOD GO_GRID->get_selected_rows
IMPORTING
et_index_rows = lt_row.

* Rows selected?.
CLEAR l_lines.
DESCRIBE TABLE lt_row LINES l_lines.

* Check if there are rows selected.
IF l_lines > 0.
CLEAR gt_ztest_selected.
LOOP AT lt_row INTO ls_row.
CLEAR gs_ztest_selected.
READ TABLE gt_ztest INTO gs_ztest_selected "Attention!!
INDEX ls_row-index.
CLEAR ls_row.
APPEND gs_ztest_selected TO gt_ztest_selected.
ENDLOOP.
ENDIF.


I think it´s ok. maybe I´ve made some mistake here but i think it works properly.

Regards form Spain,
Bye,
Copyright ?2007 - 2008 www.jt77.com