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 in ALV Grid

Former Member
0 Likes
1,077

Hi all.

I need to be able to modify the database table in ALV Grid. Here's the code. The database table, that is displayed is 'zng_so_head', it's internal table is 'res_tab'. In the program i tried to create a button 'CHANGE' on the ALV-toolbar, but when executing the program there's no such button (why?). What code, screens, screen elements, etc. should be added to this program to provide a possibility of changing data of the database table 'zng_so_head' (i mean changing existing data, adding new lines and saving changes)?

CODE:

&----


& Report ZNG_ALV_TC_EDIT_SIMP&

&----


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,

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.

*-- FILLING IN INTERNAL TABLE

SELECT hso_num hvend_num hcust_num hso_date

INTO TABLE res_tab FROM zng_so_head AS h.

&----


*& 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 = 'SO_NUM'.

ls_fcat-inttype = 'N'.

ls_fcat-ref_table = 'zng_so_head'.

ls_fcat-outputlen = '12'.

ls_fcat-coltext = 'SO_NUM'.

ls_fcat-seltext = 'SO_NUM'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

ls_fcat-fieldname = 'VEND_NUM'.

ls_fcat-inttype = 'N'.

ls_fcat-ref_table = 'zng_so_head'.

ls_fcat-outputlen = '12'.

ls_fcat-coltext = 'VEND_NUM'.

ls_fcat-seltext = 'VEND_NUM'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

ls_fcat-fieldname = 'CUST_NUM'.

ls_fcat-inttype = 'N'.

ls_fcat-ref_table = 'zng_so_head'.

ls_fcat-outputlen = '12'.

ls_fcat-coltext = 'CUST_NUM'.

ls_fcat-seltext = 'CUST_NUM'.

APPEND ls_fcat TO pt_fieldcat.

CLEAR ls_fcat.

ls_fcat-fieldname = 'SO_DATE'.

ls_fcat-inttype = 'D'.

ls_fcat-ref_table = 'zng_so_head'.

ls_fcat-outputlen = '12'.

ls_fcat-coltext = 'SO_DATE'.

ls_fcat-seltext = 'SO_DATE'.

APPEND ls_fcat TO pt_fieldcat.

ENDFORM. "prepare_field_catalog

&----


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

CALL METHOD gr_alvgrid->set_table_for_first_display

EXPORTING

is_layout = gs_layout

CHANGING

it_outtab = res_tab[]

it_fieldcatalog = gt_fieldcat.

ELSE.

CALL METHOD gr_alvgrid->refresh_table_display.

ENDIF.

ENDFORM. "display_alv

----


  • CLASS lcl_event_handler DEFINITION

----


CLASS lcl_event_handler DEFINITION.

PUBLIC SECTION.

METHODS:

handle_toolbar

FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object

e_interactive.

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.

APPEND ls_toolbar TO e_object->mt_toolbar.

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

ENDCLASS. "lcl_event_handler IMPLEMENTATION

DATA object_ref TYPE REF TO lcl_event_handler.

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.

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.

ENDIF.

ENDMODULE. "status_0100 INPUT

*----


END OF CODE

Thanks all.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,000

Hi,

For the button on the toolbar,here is the code:

CLASS lcl_eh IMPLEMENTATION.

***********************************************************************

*METHOD: HANDLE_TOOLBAR

*DESCRIPTION: This method provides the necessary detail required to

  • create an extra button in the toolbar.

***********************************************************************

METHOD handle_toolbar.

CLEAR ls_toolbar.

MOVE 'CHANGE' TO ls_toolbar-function.

MOVE 0 TO ls_toolbar-butn_type.

MOVE CHANGETO ls_toolbar-text.

MOVE 'ICON_DETAIL' TO ls_toolbar-icon.

MOVE 'CHANGE' TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD. "handle_toolbar

Also,if you are using the CHANGE button to record changes in the DB,then in the local class that you have defined,you should use the following method and then write the logic:

***********************************************************************

*METHOD: HANDLE_USER_COMMAND

***********************************************************************

METHOD handle_user_command.

CASE e_ucomm.

WHEN 'CHANGE'.

CHECK_CHANGED_DATA

REFRESH_TABLE_DISPLAY

Now call the methods that i have given in my previous post.This function code will be checked at the event you click the button 'CHANGE'.I think you have done this in an ELSE condition in the PAI of the screen.Not too sure if it works in the PAI.

Regards,

Beejal

**Reward if this helps

7 REPLIES 7
Read only

Former Member
0 Likes
1,000

In the implementation of the event handler class, one line is un-necessary:

CLASS lcl_event_handler IMPLEMENTATION.

METHOD handle_toolbar.

DATA: ls_toolbar TYPE stb_button.

MOVE 3 TO ls_toolbar-butn_type.

>>>>>>>>>>>>APPEND ls_toolbar TO e_object->mt_toolbar.<<<<<<<<<<<<

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

ENDCLASS. "lcl_event_handler IMPLEMENTATION

Read only

0 Likes
1,000

Yes thanks, it is really unnecessary, but the button is still not displayed on the toolbar.

Read only

Former Member
0 Likes
1,000

Implement an event handler method for the event user_command of cl_gui_alv_grid.

( Also SET HANDLER for this event ).

This event handler method imports e_ucomm.

Implement this method as a CASE e_ucomm. ( it will contain the value of the function code of the button pressed ). [ In your case , you have given CHANGE as the function code for your toolbar button ]

Within the appropriate WHEN, implement the logic to update your itab or the transparent table.

Message was edited by:

Arpit Sud

Read only

Former Member
0 Likes
1,000

Hi,

To modify database from the ALV grid,you need to do the following:

---You have to modify the field Catalog fields (fields that you want to make editable).Set the field EDIT as 'X'.For example if you want to make the field below editable:

ls_fcat-fieldname = 'SO_NUM'.

ls_fcat-inttype = 'N'.

ls_fcat-ref_table = 'zng_so_head'.

ls_fcat-outputlen = '12'.

ls_fcat-coltext = 'SO_NUM'.

ls_fcat-seltext = 'SO_NUM'.

ls_fcat-edit = 'X'.

APPEND ls_fcat TO pt_fieldcat.

---Call the method below before you call the refresh_table_display method.

CALL METHOD ALV_GRID_INSTANCE->set_ready_for_input

EXPORTING

i_ready_for_input = 0. ( For Display ) and ('1' for Edit )

After this put the refresh_table_display method.

Now if the ALV data has changed,and you want to change the database as well...you need an extra button.I assume that the CHANGE button that you have added is for that purpose.In the event handler method,e_ucomm is imported.You need to write the following methods in the case e_ucomm.(WHEN 'CHANGE').

--- If you want to check if the user has entered any value on the grid, use the Method : CALL METHOD gr_alvgrid->check_changed_data.

This will reflect as a change in your existiing data table to new data added on the editable grid.

For example,you can use it at user-command 'CHANGE':

WHEN 'CHANGE'.

call method gr_alvgrid->check_changed_data

importing e_valid = l_valid.

if l_valid = 'X'.

MODIFY zng_so_head FROM TABLE res_tab.

endif.

Regards,

Beejal

**Reward if this helps

Message was edited by:

Beejal Rawal

Read only

Former Member
0 Likes
1,001

Hi,

For the button on the toolbar,here is the code:

CLASS lcl_eh IMPLEMENTATION.

***********************************************************************

*METHOD: HANDLE_TOOLBAR

*DESCRIPTION: This method provides the necessary detail required to

  • create an extra button in the toolbar.

***********************************************************************

METHOD handle_toolbar.

CLEAR ls_toolbar.

MOVE 'CHANGE' TO ls_toolbar-function.

MOVE 0 TO ls_toolbar-butn_type.

MOVE CHANGETO ls_toolbar-text.

MOVE 'ICON_DETAIL' TO ls_toolbar-icon.

MOVE 'CHANGE' TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD. "handle_toolbar

Also,if you are using the CHANGE button to record changes in the DB,then in the local class that you have defined,you should use the following method and then write the logic:

***********************************************************************

*METHOD: HANDLE_USER_COMMAND

***********************************************************************

METHOD handle_user_command.

CASE e_ucomm.

WHEN 'CHANGE'.

CHECK_CHANGED_DATA

REFRESH_TABLE_DISPLAY

Now call the methods that i have given in my previous post.This function code will be checked at the event you click the button 'CHANGE'.I think you have done this in an ELSE condition in the PAI of the screen.Not too sure if it works in the PAI.

Regards,

Beejal

**Reward if this helps

Read only

0 Likes
1,000

thanks Beejal.

Your posts were really very helpful. But how should I define """""l_valid"""""

in the following code:

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 zng_so_head FROM TABLE res_tab.

endif.

ENDCASE.

ENDMETHOD. "handle_user_command

regards, Nikolai

Read only

0 Likes
1,000

Hi Nikolai,

Define l_valid in your class definition as a character:

<b>DATA:l_valid type c.</b>

It only acts as a flag.

regards,

Beejal

**Please reward if my posts are helpful

Message was edited by:

Beejal Rawal