Application Development and Automation 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: 
Read only

Modifying database table through ALV-Grid

Former Member
0 Likes
1,451

Hi all.

I need to modify a database table (ZNG_SO_HEAD) by entering data in ALV-Grid, which displays its internal table (exact copy of ZNG_SO_HEAD), and clicking the button ('CHANGE') on the ALV-toolbar. The ALV is already editable, the button already exists. Here is the code. After changing data in ALV and clicking 'CHANGE' on the toolbar the database table ZNG_SO_HEAD remains unchangeable, but i need to change data in it somehow.

Thanks all.

REPORT  zng_alv_tc_edit_simp.
*-- GLOBAL DATA DECLARATIONS FOR ALV
DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid.
DATA gc_custom_control_name TYPE scrfname VALUE 'CC_ALV'.
DATA gr_ccontainer TYPE REF TO cl_gui_custom_container.
DATA gt_fieldcat TYPE lvc_t_fcat.
DATA gs_layout TYPE lvc_s_layo.

TABLES: zng_so_head, zng_cust, zng_vendors.
*-- STRUCTURE OF INTERNAL TABLE
TYPES: BEGIN OF in_tab,
        mandt TYPE zng_so_head-mandt,
        so_num TYPE zng_so_head-so_num,          "type numc
        vend_num TYPE zng_so_head-vend_num,      "type numc
        cust_num TYPE zng_so_head-cust_num,      "type numc
        so_date TYPE zng_so_head-so_date,        "type dats
       END OF in_tab.
*-- INTERNAL TABLE HOLDING LIST DATA
DATA res_tab TYPE TABLE OF in_tab WITH HEADER LINE.
*DATA wa_res_tab LIKE LINE OF res_tab.
*-- FILLING IN INTERNAL TABLE
SELECT h~mandt h~so_num h~vend_num h~cust_num h~so_date
INTO TABLE res_tab FROM zng_so_head AS h.

*---------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS:
*   to add new functional buttons to the alv toolbar
    handle_toolbar
      FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING e_object
                  e_interactive,
*   to implement user commands
    handle_user_command
      FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING e_ucomm.
ENDCLASS.             "lcl_event_handler DEFINITION

*---------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
  METHOD handle_toolbar.
    DATA: ls_toolbar TYPE stb_button.
    MOVE 3 TO ls_toolbar-butn_type.
    CLEAR ls_toolbar.
    MOVE 'CHANGE' TO ls_toolbar-function.
    MOVE icon_change TO ls_toolbar-icon.
    MOVE 'change' TO ls_toolbar-quickinfo.
    MOVE 'change' TO ls_toolbar-text.
    APPEND ls_toolbar TO e_object->mt_toolbar.
  ENDMETHOD.                    "handle_toolbar>
  METHOD handle_user_command.
    DATA:l_valid TYPE c.
    CASE e_ucomm.
      WHEN 'CHANGE'.
        CALL METHOD gr_alvgrid->check_changed_data
          IMPORTING
            e_valid = l_valid.
        IF l_valid = 'X'.
          MODIFY zng_so_head FROM res_tab.
        ENDIF.
    ENDCASE.
  ENDMETHOD. "handle_user_command
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION

DATA object_ref TYPE REF TO lcl_event_handler.

FORM prepare_field_catalog CHANGING pt_fieldcat TYPE lvc_t_fcat.
>>>>>>done correctly>>>>>>>>>

FORM display_alv.
>>>>>>done correctly>>>>>>>>>

START-OF-SELECTION.
  CALL SCREEN 100.
*---------------------------------------------------------------------*
*  MODULE STATUS_0100 OUTPUT
*---------------------------------------------------------------------*
MODULE display_alv OUTPUT.
  SET PF-STATUS 'SCREEN_100'.
  PERFORM display_alv.
  CREATE OBJECT object_ref.
  SET HANDLER object_ref->handle_toolbar FOR gr_alvgrid.
  SET HANDLER object_ref->handle_user_command FOR gr_alvgrid.
ENDMODULE.                    "display_alv OUTPUT
*---------------------------------------------------------------------*
*  MODULE USER_COMMAND_0100 INPUT
*---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
IF sy-ucomm = 'BACK' OR
     sy-ucomm = 'EXIT' OR
     sy-ucomm = 'CANCEL'.
    LEAVE PROGRAM.
  ELSE.
    CALL METHOD object_ref->handle_toolbar.
    CALL METHOD object_ref->handle_user_command.
  ENDIF.
ENDMODULE.

1 ACCEPTED SOLUTION
Read only

andreas_mann3
Active Contributor
0 Likes
1,035

hi,

have you set break-point at:

<b>IF l_valid = 'X'.</b> ?

analyse table res_tab are there changes at all ?

A.

8 REPLIES 8
Read only

andreas_mann3
Active Contributor
0 Likes
1,036

hi,

have you set break-point at:

<b>IF l_valid = 'X'.</b> ?

analyse table res_tab are there changes at all ?

A.

Read only

0 Likes
1,035

Hi Andreas.

There are changes in res_tab (internal table), but there are no changes in zng_so_head (database table)

Read only

0 Likes
1,035

I think here I should define and implement method

data_changed_finished OF cl_gui_alv_grid

to copy the data from ALV-grid to the internal table res_tab. But I dont know exactly how to do it.

Read only

Former Member
0 Likes
1,035

Hi,

Use this program to achieve your requirement..In the parameter give your table name..Then in the output ALV grid..You can change, insert new records to your table..

PARAMETERS: p_table TYPE dd02l-tabname.

DATA: t_output LIKE se16n_output OCCURS 0 WITH HEADER LINE.

START-OF-SELECTION.

SELECT fieldname FROM dd03l INTO TABLE t_output

WHERE tabname = p_table.

IF sy-subrc <> 0.

MESSAGE s208(00) WITH 'Table not found'.

LEAVE LIST-PROCESSING.

ENDIF.

CALL FUNCTION 'SE16N_INTERFACE'

EXPORTING

i_tab = p_table

i_edit = 'X'

TABLES

it_output_fields = t_output

EXCEPTIONS

no_values = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

Thanks,

Naren

Read only

0 Likes
1,035

Thanks Naren, but I need only a method, with help of which I will be able to copy data from the ALV-grid to the internal table

res_tab

and then modify the database table from it, and the way to realize this method as well.

Read only

Former Member
0 Likes
1,035

Hello Nikolai,

I have written a sample code taking care of all the requirements(button in the toolbar and changes saved in database).I have used SPFLI table and the internal table i_spfli.This code works and the change is also made in the database table.

REPORT SAMPLE.

*-- GLOBAL DATA DECLARATIONS FOR ALV

DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid.

DATA gc_custom_control_name TYPE scrfname VALUE 'CC_ALV'.

DATA gr_ccontainer TYPE REF TO cl_gui_custom_container.

DATA gt_fieldcat TYPE lvc_t_fcat.

DATA gs_layout TYPE lvc_s_layo.

Data:i_spfli type table of spfli.

----


  • CLASS lcl_event_handler DEFINITION

----


CLASS lcl_event_handler DEFINITION.

PUBLIC SECTION.

DATA:l_valid TYPE c.

DATA: ls_toolbar TYPE stb_button.

METHODS:

  • to add new functional buttons to the alv toolbar

handle_toolbar

FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object

e_interactive,

  • to implement user commands

handle_user_command

FOR EVENT user_command OF cl_gui_alv_grid

IMPORTING e_ucomm.

ENDCLASS. "lcl_event_handler DEFINITION

----


  • CLASS lcl_event_handler IMPLEMENTATION

----


CLASS lcl_event_handler IMPLEMENTATION.

METHOD handle_toolbar.

CLEAR ls_toolbar.

MOVE 0 TO ls_toolbar-butn_type.

MOVE 'CHANGE' TO ls_toolbar-function.

MOVE 'ICON_CHANGE' TO ls_toolbar-icon.

MOVE 'change' TO ls_toolbar-quickinfo.

MOVE 'change' TO ls_toolbar-text.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD. "handle_toolbar>

METHOD handle_user_command.

CASE e_ucomm.

WHEN 'CHANGE'.

CALL METHOD gr_alvgrid->check_changed_data

IMPORTING

e_valid = l_valid.

IF l_valid = 'X'.

MODIFY spfli FROM table i_spfli.

ENDIF.

ENDCASE.

ENDMETHOD. "handle_user_command

ENDCLASS. "lcl_event_handler IMPLEMENTATION

START-OF-SELECTION.

DATA object_ref TYPE REF TO lcl_event_handler.

select * from spfli into table i_spfli.

Call screen 100.

----


  • MODULE STATUS_0100 OUTPUT

----


MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'SCREEN_100'.

PERFORM display_alv.

ENDMODULE. "display_alv OUTPUT

----


  • MODULE USER_COMMAND_0100 INPUT

----


MODULE user_command_0100 INPUT.

IF sy-ucomm = 'BACK' OR

sy-ucomm = 'EXIT' OR

sy-ucomm = 'CANCEL'.

LEAVE PROGRAM.

ENDIF.

ENDMODULE.

&----


*& Form display_alv

&----


FORM display_alv.

*IF gr_alvgrid IS INITIAL.

CREATE OBJECT gr_ccontainer

EXPORTING container_name = gc_custom_control_name.

CREATE OBJECT gr_alvgrid

EXPORTING i_parent = gr_ccontainer.

PERFORM prepare_field_catalog CHANGING gt_fieldcat.

CREATE OBJECT object_ref.

SET HANDLER object_ref->handle_toolbar FOR gr_alvgrid.

SET HANDLER object_ref->handle_user_command FOR gr_alvgrid.

CALL METHOD gr_alvgrid->set_table_for_first_display

EXPORTING

is_layout = gs_layout

CHANGING

it_outtab = i_spfli[]

it_fieldcatalog = gt_fieldcat.

ENDFORM. "display_alv

&----


*& Form prepare_field_catalog

&----


FORM prepare_field_catalog CHANGING pt_fieldcat TYPE lvc_t_fcat.

DATA ls_fcat TYPE lvc_s_fcat.

ls_fcat-fieldname = 'CARRID'.

ls_fcat-ref_table = 'SPFLI'.

ls_fcat-edit = 'X'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

ls_fcat-fieldname = 'CONNID'.

ls_fcat-ref_table = 'SPFLI'.

ls_fcat-edit = 'X'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

ls_fcat-fieldname = 'DEPTIME'.

ls_fcat-ref_table = 'SPFLI'.

ls_fcat-edit = 'X'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

ls_fcat-fieldname = 'ARRTIME'.

ls_fcat-ref_table = 'SPFLI'.

ls_fcat-edit = 'X'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

endform.

Hope this helps.

Regards,

Beejal

Read only

0 Likes
1,035

Thanks Beejal,

I tried your code, but the program works as before, there are no changes in database table.

regards, Nikolai

Read only

0 Likes
1,035

Hello,

have you tried COMMIT WORK after the MODIFY statement?

Regards,

Melina