Skip to Content

abt editable ALV column and database updating

How to check the changed rows and update database in editable ALV?

As far as I consider, if the numbers of ALV rows is large, you don't need update all the rows but changed ones.

Is there any easy way to do that?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 20, 2013 at 05:15 AM

    You can copy the contents to a table and then update the ALV output table. Compare the two tables. In case of change, update to database.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    I_CALLBACK_PROGRAM = SY-REPID

    IS_LAYOUT = L_LAYOUT

    I_CALLBACK_USER_COMMAND = 'USER_COMMAND'

    IT_FIELDCAT = IT_FIELDCAT

    TABLES

    T_OUTTAB = ITAB

    EXCEPTIONS

    PROGRAM_ERROR = 1

    OTHERS = 2.

    FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM

    RS_SELFIELD TYPE SLIS_SELFIELD.

    DATA: GD_REPID LIKE SY-REPID,

    REF_GRID TYPE REF TO CL_GUI_ALV_GRID.

    ITAB1[ ] = ITAB.

    IF REF_GRID IS INITIAL.

       CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'

          IMPORTING

                  E_GRID = REF_GRID.

    ENDIF.

    IF NOT REF_GRID IS INITIAL.

         CALL METHOD REF_GRID->CHECK_CHANGED_DATA .

    ENDIF.

    LOOP AT ITAB INTO WA_ITAB.

        READ ITAB1 INTO WA_ITAB1 KEY "provide the key using the key non editable columns .

       if wa_itab1 <> wa_itab.

            perform save_to_database.

       endif.  

    endloop.

    RS_SELFIELD-refresh = 'X'.

    ENDFORM. "USER_COMMAND

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 20, 2013 at 03:33 AM

    Hi Ming,

    Are you using CL_GUI_ALV_GRID. I thought so since you mentioned editable ALV.

    If so, you might be able to do it this way.

    Approach-1:

    Step-1: Before you output the content of final internal table (itab_final) into ALV, duplicate it into another internal table (say: itab_copy).

    Step-2: After the data is displayed on the output, user changes the data and saves it. Through method GET_GRID_MODIFIED (class: CL_GUI_ALV_GRID), you can verify if the data on the grid was actually changed.

    Step-3: If it is modified, you can use the method SAVE_DATA (of same class) to transfer the data from the grid back to your internal table itab_final.

    Now, you can compare both old and new data and determine the delta i.e. the rows that were actually changed.

    Approach-2:

    Alternatively, there is also a method GET_MODIFIED_CELLS (in same class) that will return you which rows & columns were changed. This will be returned in a table format, so it should contain a list of all different cells (rows/columns) that were changed.

    You can use to information to update your table.

    HTH.

    Saurabh

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      thank you very much for you reply.

      The reason that I use "REUSE_ALV_GRID_DISPLAY_LVC" is that I am more familiar with that and I have a code pattern for this kind of ALV output method.

      If I choose OO ALV rather than ALV LVC, I have to change a lot of coding.

      The reply written by susmitha susan thomas seems like to offer an alternative solution.

      LOOP AT ITAB INTO WA_ITAB.

          READ ITAB1 INTO WA_ITAB1 KEY "provide the key using the key non editable columns .

         if wa_itab1 <> wa_itab.

              perform save_to_database.

         endif. 

      ENDLOOP.

      I have used this coding and it seems to work.

      But as far as I tried, if you set column attribute as "editable" in layout setting, change the value of any cell in ALV output and then double click, the changed value does transfer to the internal table. This is why I can compare two internal tables and know which rows are changed.

      I am wondering if you can explain more detail for me with it.

  • Oct 20, 2013 at 06:25 AM

    Hi Ming,

    Once go through the following thread, It may help you.

    Thanks & Regards,

    -Vijay

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 21, 2013 at 11:58 AM

    HI Ming,

    Try executing the below code. I have used DATA_CHANGED event to capture the modified data only.

    TYPE-POOLS: SLIS.

    data: er_data_changed type ref to cl_alv_changed_data_protocol.

    FIELD-SYMBOLS: <zfield> TYPE ANY.

    DATA: T_SFLIGHT     TYPE STANDARD TABLE OF SFLIGHT.

    DATA: IS_LAYOUT_LVC TYPE  LVC_S_LAYO.

    DATA: IT_EVENTS     TYPE  SLIS_T_EVENT.

    DATA: LT_EVENT      TYPE  SLIS_ALV_EVENT.

    DATA: L_REPORT      TYPE SY-REPID.



    START-OF-SELECTION.

      IS_LAYOUT_LVC-EDIT = 'X'.

      LT_EVENT-NAME      = 'DATA_CHANGED'.

      LT_EVENT-FORM      = 'ALV_DATA_CHANGED'.

      APPEND LT_EVENT TO IT_EVENTS.

      L_REPORT = SY-REPID.



      SELECT * UP TO 10 ROWS INTO TABLE T_SFLIGHT

        FROM SFLIGHT.



      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'

        EXPORTING

          I_CALLBACK_PROGRAM = L_REPORT

          I_STRUCTURE_NAME   = 'SFLIGHT'

          IS_LAYOUT_LVC      = IS_LAYOUT_LVC

          IT_EVENTS          = IT_EVENTS

        TABLES

          T_OUTTAB           = T_SFLIGHT.



    FORM ALV_DATA_CHANGED  USING

             ER_DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.



      DATA: LS_GOOD TYPE LVC_S_MODI.

    ASSIGN er_data_changed->mp_mod_rows->* TO <zfield>.

      LOOP AT ER_DATA_CHANGED->MT_GOOD_CELLS INTO LS_GOOD.

        MESSAGE I208(00) WITH 'Data changed'.

        EXIT.

      ENDLOOP.

    ENDFORM.                    "ALV_DATA_CHANGED

    Regards,

    Sivaganesh

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 06, 2013 at 03:47 PM

    Sorry for late reply, I've tried this again these days.

    I am now assure that you DO NOT need some code  like:

    DATA: GD_REPID LIKE SY-REPID,

    REF_GRID TYPE REF TO CL_GUI_ALV_GRID.

    ITAB1 = ITAB.

    IF REF_GRID IS INITIAL.

       CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'

          IMPORTING

                  E_GRID = REF_GRID.

    ENDIF.

    IF NOT REF_GRID IS INITIAL.

         CALL METHOD REF_GRID->CHECK_CHANGED_DATA .

    ENDIF.

    to transfer the changed data into the internal table.

    What you need is just write code in user_command form like:


    FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM

                             RS_SELFIELD TYPE SLIS_SELFIELD .

    // your code here

    ENDFORM.

    And  this Form comes from either "I_CALLBACK_USER_COMMAND   = 'USER_COMMAND'"

    or "IT_EVENTS = IT_EVENTS" which are the parameter of ALV FM.

    However, this method does not work very good, sometimes the data on the table changed, but does not transfer to the internal table if you don't double click or click save button strictly.

    Add comment
    10|10000 characters needed characters exceeded