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

alv grid OO runtime error after adding new rows

Former Member
0 Likes
2,024

Hi everybody,

I have a problem with alv OO.

I want to add an empty and editable row to my alv grid and save it after the user filled it.

I have my alv grid:

 
  IF G_CUSTOM_CONTAINER IS INITIAL.
    CREATE OBJECT G_CUSTOM_CONTAINER
      EXPORTING
        CONTAINER_NAME = G_CONTAINER.
    CREATE OBJECT GRID1
      EXPORTING
        I_PARENT = G_CUSTOM_CONTAINER.

    CALL METHOD grid1->set_ready_for_input
      EXPORTING
        i_ready_for_input = 1. 
    CALL METHOD GRID1->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        it_toolbar_excluding = it_exclude
        is_layout = ls_layout
      CHANGING
        IT_OUTTAB            = it_outtab
        it_fieldcatalog      = it_fieldcat[].


    CREATE OBJECT event_receiver.
    SET HANDLER event_receiver->handle_user_command FOR grid1.
    SET HANDLER event_receiver->handle_toolbar FOR grid1.

    CALL METHOD grid1->set_toolbar_interactive.

Then i have a button, that adds a new row to my grid:

      

METHOD handle_user_command.
DATA: lt_rows TYPE lvc_t_row.
 CASE e_ucomm.
 when 'newline'.
        wa_outtab-field1= ''.
        wa_outtab-field2 = ''.
        wa_outtab-field3 = ''.
        wa_outtab-field4 = ''.
        wa_outtab-flag = 'N'.
        ls_celltab-Style = GRID1->mc_style_enabled.
        CLEAR wa_outtab-Style.

        ls_celltab-fieldname = 'field1'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'field2'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'field3'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'field4'.
        insert ls_celltab into table wa_outtab-Style.
        append wa_outtab to IT_OUTTAB.
        clear wa_outtab.
        Call METHOD GRID1->refresh_table_display. 

Now the problem is, i can add a row, but as soon as i fill it, i get a runtime error for any user_command.

So for example:

1. I add an empty row

2. I fill it.

3. I want to add another row --> runtimer error:

Trigger Location of Runtime Error

Program CL_GUI_ALV_GRID===============CP

Include CL_GUI_ALV_GRID===============CM031

Row 96

Module type (METHOD)

Module Name FOREIGN_KEY_CHECK

   

   66                        OF STRUCTURE <ls_ref_table> TO <l_field_to>.
   67
   68       CLEAR lflg_moved.
   69       CLEAR l_no_check.
   70
   71       READ TABLE ct_mod_cells INTO ls_mod_cells
   72                               WITH KEY fieldname = ls_fields-fieldname
   73                                        row_id    = i_row_id.
   74
   75       IF sy-subrc EQ 0.
   76         l_changed_field = ls_fields-fieldname.
   77
   78         READ TABLE m_cl_variant->mt_fieldcatalog INTO l_fcat WITH KEY
   79                  fieldname = ls_fields-fieldname.
   80         IF ls_mod_cells-value IS INITIAL AND l_fcat-no_init_ch ca 'XB'.
   81           l_no_check = 'X'.
   82         ELSEIF ls_mod_cells-error EQ 'X'.
   83           <l_field_to> = ls_mod_cells-value.
   84           lflg_moved = 'X'.
   85         ENDIF.
   86       ENDIF.
   87
   88       IF l_no_check = 'X'.
   89         MOVE-CORRESPONDING ls_mod_cells TO ls_good_cells.
   90         APPEND ls_good_cells TO ct_good_cells.
   91       ELSE.
   92         IF lflg_moved IS INITIAL.
   93           ASSIGN COMPONENT ls_fields-fieldname
   94                            OF STRUCTURE cs_wa TO <l_field_from>.
   95           IF sy-subrc NE 0.
>>>>>             MESSAGE x000(0k).
   97           ENDIF.
   98           <l_field_to> = <l_field_from>.
   99         ENDIF.
  100
  101 *... does checktable contain other key fields which need to be
  102 *    considered
  103         MOVE ls_check-ref_table  TO ls_buf_id-tabname.
  104         MOVE ls_fields-fieldname TO ls_buf_id-fieldname.
  105
  106         MOVE ls_check-ref_table  TO l_tbfk_tabname.
  107         IF ls_fields-ref_field IS INITIAL.
  108           MOVE ls_fields-fieldname TO l_tbfk_fieldname.
  109         ELSE.
  110           MOVE ls_fields-ref_field TO l_tbfk_fieldname.
  111         ENDIF.
  112
  113         IMPORT dd05m_tab TO lt_dd05m
  114                dd08v_tab TO lt_dd08v
  115                timestamp TO ls_timestamp 

First i thought the Problem is that i fill in wrong data (because of "Foreign_key_check"), but thats definitivly not the problem, because if i add the new row with default values like this, i can save it without any problems. But if i edit any of these cells, i get an error again, so the problem has nothing to do with the content of the cells.

      

METHOD handle_user_command.
DATA: lt_rows TYPE lvc_t_row.
 CASE e_ucomm.
 when 'newline'.
        wa_outtab-field1= 'Text'.
        wa_outtab-field2 = '12345'.
        wa_outtab-field3 = '100'.
        wa_outtab-field4 = 'AB'.
        wa_outtab-flag = 'N'.
        ls_celltab-Style = GRID1->mc_style_enabled.
        CLEAR wa_outtab-Style.

        ls_celltab-fieldname = 'field1'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'field2'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'field3'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'field4'.
        insert ls_celltab into table wa_outtab-Style.
        append wa_outtab to IT_OUTTAB.
        clear wa_outtab.
        Call METHOD GRID1->refresh_table_display. 

Does anyone have an idea where's my mistake?

Thanks for your help.

Cheers

DTox2k

Edited by: DTox2k on Oct 21, 2008 9:08 AM

Edited by: DTox2k on Oct 21, 2008 9:18 AM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,090

why are you using the fieldnames in small letters. use CAPS

ls_celltab-fieldname = 'field1'.

ls_celltab-fieldname = 'FIELD1'.

6 REPLIES 6
Read only

Former Member
0 Likes
1,091

why are you using the fieldnames in small letters. use CAPS

ls_celltab-fieldname = 'field1'.

ls_celltab-fieldname = 'FIELD1'.

Read only

0 Likes
1,090

thanks for your answer!

you're right, my mistake, in my coding all fieldnames are in caps, else the cells wouldn't be editable, i just changed the fielddnames here to make it easier to read.

Edited by: DTox2k on Oct 21, 2008 10:18 AM

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,090

Hello

The solution for your problem has been described in sample report ZUS_SDN_ALVGRID_EDITABLE_8A in thread

Regards

Uwe

Read only

0 Likes
1,090

Hi Uwe Schieferstein,

thanks for your answer.

i tried to understand your report (im new to ABAP OO and its my first report with an alv grid ) and implement your solution, but it still doesn't work:

First i replace the function code:

  METHOD handle_toolbar.
* § 2.In event handler method for event TOOLBAR: Append own functions
*   by using event parameter E_OBJECT.
    DATA: ls_toolbar  TYPE stb_button.

    LOOP AT e_object->mt_toolbar INTO ls_toolbar.
      CASE ls_toolbar-function.
        WHEN cl_gui_alv_grid=>mc_fc_loc_delete_row.
          ls_toolbar-function = 'DELETE_ROW'.
          MODIFY e_object->mt_toolbar FROM ls_toolbar INDEX syst-tabix.

        WHEN cl_gui_alv_grid=>MC_FC_LOC_COPY_ROW.
          ls_toolbar-function = 'ADD_ROW'.
          MODIFY e_object->mt_toolbar FROM ls_toolbar INDEX syst-tabix.
        WHEN OTHERS.
          CONTINUE.
      ENDCASE.

    ENDLOOP. 

then trigger PAI:

      
METHOD handle_user_command.
DATA: lt_rows TYPE lvc_t_row.
CASE e_ucomm.
when 'ADD_ROW'.

      when OTHERS.
        return.
    ENDCASE.
        CALL METHOD sender->get_selected_rows
         IMPORTING
           et_index_rows = mt_sel_rows
*        et_row_no     =
           .


    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = e_ucomm.

        .
  ENDMETHOD.                           "handle_user_command

And finally my PAI Module:

MODULE PAI INPUT.
*   to react on oi_custom_events:
  call method cl_gui_cfw=>dispatch.
  CASE OK_CODE.
    WHEN 'EXIT'.
      PERFORM EXIT_PROGRAM.
    when 'ADD_ROW'.

              DATA: ld_next   TYPE i,
         ls_row    TYPE lvc_s_row.


        SORT event_receiver->mt_sel_rows BY index DESCENDING. " !!!

        LOOP AT event_receiver->mt_sel_rows INTO ls_row.
          READ TABLE it_outtab INTO wa_outtab INDEX ls_row-index.

          CLEAR: wa_outtab-FIELD1. 
          CLEAR: wa_outtab-FIELD2.
          CLEAR: wa_outtab-FIELD3.
          CLEAR: wa_outtab-FIELD4.
          wa_outtab-flag = 'N'.
          ls_celltab-Style = GRID1->mc_style_enabled.
        CLEAR wa_outtab-Style.
* Alle Spalten der neuen Zeile auf editierbar einstellen
        ls_celltab-fieldname = 'FIELD1'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'FIELD2'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'FIELD3'.
        insert ls_celltab into table wa_outtab-Style.
        ls_celltab-fieldname = 'FIELD4'.
        insert ls_celltab into table wa_outtab-Style.
          ld_next = ls_row-index + 1.
          INSERT wa_outtab INTO it_outtab INDEX ld_next.
        ENDLOOP.
        Call METHOD GRID1->refresh_table_display.
    WHEN OTHERS.
*     do nothing
  ENDCASE.
  CLEAR OK_CODE.
ENDMODULE.    

The only difference to your code is that i had to call refresh_table_display, else the new row doesn't appear.

But i still have the same problem now, as soon as i edit a cell, i get the same runtime error for any user_command then before.

Cheers

DTox2k

Read only

Former Member
0 Likes
1,090

I continued testing my program and i now think the problem is not how i add an row, it must be more general, because i have the same Problem with standard alv functions, so for example i have a button, that makes one cell of all selected rows editiable:

      WHEN 'change'.


        call METHOD GRID1->GET_SELECTED_ROWS
          IMPORTING
            et_index_rows = it_index_rows.
        clear wa_outtab-Style.
        ls_celltab-fieldname = 'ZZTXT'.
        ls_celltab-Style = GRID1->mc_style_enabled.
        loop at it_index_rows into wa_index_rows.
          read TABLE it_outtab into wa_outtab INDEX wa_index_rows-INDEX.
          if ( wa_outtab-lock <> 'X' and wa_outtab-lock <> 'N' ).
            if ( wa_outtab-Style[] is not initial ).
              modify wa_outtab-Style from ls_celltab INDEX 1.
            else.
              insert ls_celltab into table wa_outtab-Style.
            endif.
            wa_outtab-lock = 'X'.
            modify it_outtab from wa_outtab INDEX wa_index_rows-INDEX.

          endif.
          clear wa_outtab.
        endloop.
* Refresh des Grids
        Call METHOD GRID1->refresh_table_display.

This button works perfectly for all rows that are on my grid for the first display. Now, if i duplicate one of these rows (with MC_FC_LOC_COPY_ROW) and try to use my this button for the duplicated row, i get this runtime error again.

Any ideas what im doing wrong?

Cheers

DTox2k

Read only

Former Member
0 Likes
1,090

I solved the Problem by myself.

My table it_outtab didn't have a column MANDT, this caused the "foreign_key_check"-error as soon as i changed any cell of a new row.