Here's how to do ALV (OO) with dynamic fcat, int table and editable data

Hi everybody
Here's a more useful approach to ALV grid with OO using dynamic table, data NOT from DDIC, dynamic FCAT and how to get changed lines from the grid when ENTER key is pressed.
It's really not too dificult but I think this is more useful than the ever present SFLIGHT methods from the demos.
This also defines a subclass of cl_gui_alv_grid so you can access the protected attributes / methods of that class.
You don't need to add the class via SE24 -- done fron this ABAP.
When you run it click Edit for the first time.
After editing data press ENTER and the break point should bring you into the relevant method.
Code developed on NW2004S trial version but also works on rel 6.40 on a "Real" system.
The code should work without any changes on any system >=6.40.
All you need to do is to create a blank screen 100 via SE51  with a custom container on it called CCONTAINER1.
The rest of the code can just be uploaded into your system using the SE38 upload facility.
When running the program click on the EDIT button to enable the edit functionality of the grid.
Change your data and when you press ENTER you should get the break-point where you can see the original table and changed rows.
This program is actually quite general as it covers Dynamic tables, building a dynamic fcat where your table fields are NOT in the DDIC, intercepting the ENTER key via using an event, and accessing the protected attributes of the cl_gui_alv_grid by defining a subclass of this class in the abap.
I've seen various questions relating to all these functions but none in my view ever answers the questions in a simple manner. I hope this simple program will answer all these and show how using OO ALV is actually quite easy and people shouldn't be scared of using OO.
Have fun and award points if useful.
Cheers
Jimbo.
<b>PROGRAM zdynfieldcat.
Simple test of dynamic ITAB with user defined (not ddic) fields
Build dynamic fcat
use ALV grid to display and edit.
*When edit mode set to 1 toolbar gives possibility of adding and
*deleting rows.
*Define subclass of cl_gui_alv_grid so we can use protected attributes
*and methods.
Add event handler to intercept user entering data and pressing the
*ENTER key.
When enter key is pressed get actual value of NEW table (all rows)
rather than just the changed data.
*use new RTTI functionality to retrieve internal table structure
*details.
Create a blank screen 100  with a custom container called CCONTAINER1.
James Hawthorne
include <icon>.
define  any old internal structure  NOT in DDIC
types: begin of s_elements,
       anyfield1(20) type c,
       anyfield2(20) type c,
       anyfield3(20) type c,
       anyfield4(20) type c,
       anyfield5(11) type n,
       end of s_elements.
types:  lt_rows  type lvc_t_roid.
Note new RTTI functionality allows field detail retrieval
at runtime for dynamic tables.
data:   wa_element type s_elements ,
        wa_data type s_elements,
        c_index type sy-index,
        c_dec2 type s_elements-anyfield5,
        wa_it_fldcat type lvc_s_fcat,
        it_fldcat type lvc_t_fcat,
        lr_rtti_struc TYPE REF TO cl_abap_structdescr,    "RTTI
        lt_comp TYPE cl_abap_structdescr=>component_table,"RTTI
        ls_comp LIKE LINE OF lt_comp,                     "RTTI
        zog  like line of lr_rtti_struc->components,      "RTTI
        struct_grid_lset type lvc_s_layo,
        l_valid  type c,
        new_table type ref to data.
field-symbols: <dyn_table> type standard table,
               <actual_tab> type standard table,
               <fs1> type ANY,
               <FS2> TYPE TABLE.
data: grid_container1 type ref to cl_gui_custom_container.
class lcl_grid_event_receiver definition deferred.
data: g_event_receiver type ref to lcl_grid_event_receiver.
data: ls_modcell type LVC_S_MODI,
      stab type ref to data,
      sdog type  s_elements.      .
class lcl_grid_event_receiver definition.
  public section.
    methods:
    handle_data_changed
         for event data_changed of cl_gui_alv_grid
             importing er_data_changed,
       toolbar for event toolbar of cl_gui_alv_grid
                 importing e_object
                           e_interactive,
      user_command for event user_command of cl_gui_alv_grid
                 importing e_ucomm.
endclass.
*implementation of Grid event-handler class
class lcl_grid_event_receiver implementation.
method handle_data_changed.
code whatever required after data entry.
various possibilites here as you can get back Cell(s) changed
columns or the entire updated table.
Data validation is also possible here.
perform check_data using er_data_changed.
endmethod.
Method for handling all creation/modification calls to the toolbar
  method toolbar.
    data : ls_toolbar type stb_button.
Define Custom Button in the toolbar
    clear ls_toolbar.
    move 0 to ls_toolbar-butn_type.
    move 'EDIT' to ls_toolbar-function.
    move space to ls_toolbar-disabled.
    move 'Edit' to ls_toolbar-text.
    move icon_change_text to ls_toolbar-icon.
    move 'Click2Edit' to ls_toolbar-quickinfo.
    append ls_toolbar to e_object->mt_toolbar.
    clear ls_toolbar.
    move 0 to ls_toolbar-butn_type.
    move 'UPDA' to ls_toolbar-function.
    move space to ls_toolbar-disabled.
    move 'Update' to ls_toolbar-text.
    move icon_system_save to ls_toolbar-icon.
    move 'Click2Update' to ls_toolbar-quickinfo.
    append ls_toolbar to e_object->mt_toolbar.
    clear ls_toolbar.
    move 0 to ls_toolbar-butn_type.
    move 'EXIT' to ls_toolbar-function.
    move space to ls_toolbar-disabled.
    move 'Exit' to ls_toolbar-text.
    move icon_system_end to ls_toolbar-icon.
    move 'Click2Exit' to ls_toolbar-quickinfo.
    append ls_toolbar to e_object->mt_toolbar.
  endmethod.
  method user_command.
    case e_ucomm .
      when 'EDIT'.          "From Tool bar
        perform set_input.
         perform init_grid.
      when 'UPDA'.          "From Tool bar
        perform refresh_disp.
        perform update_table.
      when 'EXIT'.          "From Tool bar
        leave program.
    endcase.
  endmethod.
endclass.
class zcltest definition inheriting from  cl_gui_alv_grid.
define this as a subclass so we can access the protected attributes
of the superclass cl_gui_alv_grid
public section.
methods: constructor, disp_tab.
endclass.
need this now to instantiate object
as we are using subclass rather than the main cl_gui_alv_grid.
class zcltest implementation.
METHOD constructor.
CALL METHOD super->constructor
        exporting i_appl_events = 'X'
           i_parent = grid_container1.
endmethod.
method disp_tab.
FIELD-SYMBOLS: <outtab> TYPE STANDARD TABLE.
break-point 1.
mt_outtab is the data table held as a protected attribute
in class cl_gui_alv_grid.
ASSIGN me->mt_outtab->* TO <outtab>.  "Original data
do whatever you want with <outtab>
contains data BEFORE changes each time.
Note that NEW (Changed) table has been obtained already by
call to form check_data USING P_ER_DATA_CHANGED
         TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
Entered data is in table defined by <fs2>
In this method you can compare original and changed data.
Easier than messing around with individual cells.
do what you want with data in <fs2>  validate / update / merge etc
endmethod.
endclass.
data :
    ok_code like sy-ucomm,
    save_ok like sy-ucomm,
    i4 type int4,
Container Object [grid_container]
now created via method constructor
in the subclass zcltest.
Control Object [grid]
grid1 type ref to zcltest,
Event-Handler Object [grid_handler]
grid_handler type ref to lcl_grid_event_receiver.
start-of-selection.
call screen 100.
module status_0100 output.
now display it as grid
if grid_container1 is initial.
    create object grid_container1
        exporting
          container_name = 'CCONTAINER1'.
    create object grid1.
     break-point 1.
    create object grid_handler.
    set handler:
       grid_handler->user_command for grid1,
       grid_handler->toolbar for grid1,
       grid_handler->handle_data_changed for grid1.
perform create_dynamic_fcat.
perform create_dynamic_itab.
perform populate_dynamic_itab.
perform init_grid.
perform register_enter_event.
set off ready for input initially
i4 = 0.
  call method grid1->set_ready_for_input
         exporting
           i_ready_for_input = i4.
endif.
endmodule.
module user_command_0100 input.
*PAI not needed in OO ALV anymore as User Commands are handled as events
*in method user_command.
*we can also get control if the Data entered and the ENTER is pressed by
*raising an event.
Control then returns to method handle_data_changed.
endmodule.
form create_dynamic_fcat.
get structure of our user table for building field catalog
Use the RTTI functionality
lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( wa_data ).
Build field catalog just use basic data here
colour specific columns as well
loop at lr_rtti_struc->components into zog.
c_index = c_index + 1.
clear wa_it_fldcat.
  wa_it_fldcat-fieldname = zog-name .
  wa_it_fldcat-datatype =  zog-type_kind.
  wa_it_fldcat-inttype =   zog-type_kind.
  wa_it_fldcat-intlen =    zog-length.
  wa_it_fldcat-decimals =  zog-decimals.
  wa_it_fldcat-lowercase = 'X'.
  if c_index eq 2.
  wa_it_fldcat-emphasize = 'C411'.
     endif.
    if c_index eq 3.
  wa_it_fldcat-emphasize = 'C511'.
   endif.
  append wa_it_fldcat to it_fldcat .
endloop.
endform.
form create_dynamic_itab.
Create dynamic internal table and assign to field sysmbol.
Use dynamic field catalog just built.
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.
assign new_table->* to <dyn_table>.
endform.
form populate_dynamic_itab.
load up a line of the dynamic table
c_dec2 = c_dec2 + 11.
wa_element-anyfield1 = 'Tabbies'.
wa_element-anyfield2 = 'ger.shepards'.
wa_element-anyfield3  = 'White mice'.
wa_element-anyfield4 =  'Any old text'.
wa_element-anyfield5 =  c_dec2.
append  wa_element to <dyn_table>.
endform.
form check_data USING P_ER_DATA_CHANGED
           TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
Get altered data back
  ASSIGN   p_er_data_changed->mp_mod_rows TO <FS1>.
stab =       p_er_data_changed->mp_mod_rows.
ASSIGN STAB->* TO <FS2>.
LOOP AT <FS2> INTO sdog.
ALV grid display with altered data is now in <fs2>.
do any extra processing you want here
endloop.
now display new table
call method grid1->disp_tab.
endform.
form exit_program.
  call method grid_container1->free.
  call method cl_gui_cfw=>flush.
  leave program.
endform.
form refresh_disp.
  call method grid1->refresh_table_display.
endform.
form update_table.
The dynamic table here is the changed table read from the grid
after user has changed it
Data can be saved to DB or whatever.
loop at <dyn_table> into wa_element.
do what you want with the data here
endloop.
switch off edit mode again for next function
i4 = 0.
  call method grid1->set_ready_for_input
      exporting
          i_ready_for_input = i4.
endform.
form set_input.
i4 = 1.
  call method grid1->set_ready_for_input
     exporting
       i_ready_for_input = i4.
endform.
form switch_input.
if i4 = 1.
i4 = 0.
else.
i4 = 1.
endif.
  call method grid1->set_ready_for_input
     exporting
       i_ready_for_input = i4.
endform.
form init_grid.
Enabling the grid to edit mode,
     struct_grid_lset-edit = 'X'. "To enable editing in ALV
     struct_grid_lset-grid_title  = 'Jimbos Test'.
     call method grid1->set_table_for_first_display
       exporting
         is_layout           = struct_grid_lset
       changing
         it_outtab             =  <dyn_table>
         it_fieldcatalog       =  it_fldcat.
endform.
form register_enter_event.
call method grid1->register_edit_event
               exporting
                  i_event_id = cl_gui_alv_grid=>mc_evt_enter.
Instantiate the event or it won't work.
create object g_event_receiver.
set handler g_event_receiver->handle_data_changed for grid1.
endform.</b>

Hi there
IE7 doesn't give me the add new page option and I get 404 error when trying to access the "How to contribute" section.
I'll load up Firefox later (this browser usually works when IE7 doesn't always work properly).
I'll copy the stuff to the wiki when I've got the browser sorted out.
Cheers
jimbp

Similar Messages

  • How to add a new column to a existing table and add data to this column?

    I have a table with about 10 millions row and I want to add a new column to this table, then fill this new column with prepared data.
    How to do it ?
    Thank you

    Can I use insert statement to add the data to new-added column?No.
    07:19:37 oracle >create table test (x number);
    Table created.
    Elapsed: 00:00:03.05
    07:19:53 oracle >
    07:19:53 oracle >
    07:19:53 oracle >insert into test values (1);
    1 row created.
    Elapsed: 00:00:00.00
    07:20:01 oracle >insert into test values (2);
    1 row created.
    Elapsed: 00:00:00.00
    07:20:10 oracle >insert into test values (3);
    1 row created.
    Elapsed: 00:00:00.00
    07:20:12 oracle >
    07:20:13 oracle >commit;
    Commit complete.
    Elapsed: 00:00:00.00
    07:20:14 oracle >
    07:20:19 oracle >select * from test;
             X
             1
             2
             3
    Elapsed: 00:00:00.00
    07:20:22 oracle >
    07:20:36 oracle >alter table test add (y number);
    Table altered.
    Elapsed: 00:00:00.05
    07:20:41 oracle >
    07:20:41 oracle >
    07:20:41 oracle >
    07:20:41 oracle >select * from test;
             X          Y
             1
             2
             3
    Elapsed: 00:00:00.00
    07:20:43 oracle >
    07:20:44 oracle >
    07:20:44 oracle >update test set y=1;
    3 rows updated.
    Elapsed: 00:00:00.02
    07:20:52 oracle >commit;
    Commit complete.
    Elapsed: 00:00:00.00
    07:20:56 oracle >select * from test;
             X          Y
             1          1
             2          1
             3          1
    Elapsed: 00:00:00.00
    07:20:58 oracle >Anand

  • How to get ALV Display with First column alone in sort

    How to get ALV Display with First column alone in sort

    HI,
    You can build Internal Table and send this to the parameter "IT_SORT".
    eg:
    "the sorting Internal Table structure is as whown below.
    DATA:  t_sort_info type slis_t_sortinfo_alv.
    "Build the Sort Internal Table
      t_sort_info-fieldname = 'CARRID'.
      t_sort_info-subtot = 'X'.
      append t_sort_info.
      t_sort_info-fieldname = 'FLDATE'.
      t_sort_info-subtot = 'X'.
      append t_sort_info.
    Then pass this "IT_SORT_INFO" table to the Function module "Reuse_alv_*". (Note send the body of the Internal table only like "<b>IT_SORT = IT_SORT_INFO[]</b>".
    Here i am making ALV output sorted on CARRID & FLDATE.
    You can specify only the First Column name for sorting.
    Regards,
    Manju
    Message was edited by:
            MANJUNATHA KS

  • Out of nowhere my daughter, who uses a Samsung Galaxy Victory phone is no longer receiving my text messages but I get hers.  How can I fix this.  I have a 4S and have updated to the 1SO7.

    Out of nowhere my daughter, who uses a Samsung Galaxy Victory phone is no longer receiving my text messages but I get hers.  How can I fix this.  I have a 4S and have updated to the ios 7.0.4.

    http://support.apple.com/kb/ts2755
    if the link provided doesn't help, then you will need to contact your phone carrier as SMS is a carrier feature.

  • HT1430 I was given an ipad 2.  How do I reconfigure it with my own settings, etc. and erase the previous owners informations?

    I was given an ipad 2.  How do I reconfigure it with my own settings, etc. and erase the previous owners informations?

    But note, if the previous owner turned on Find My in iCloud and has not turned that off you will not be able to do anything with that iPad without the previous owner's Apple ID...there is an Activation Lock as part of iOS 7.0.4 that cannot be bypassed so you have to have that Apple ID and password if it has been turned on.

  • How to share a project with a non-Adobe user and present it to prospect customers?

    I Have to share my project with a person who will present it to prospect customers. I cannot ask this person to open an Adobe account. Is there a way for him to access my DPS folio and show it? Or can I export it in a way that allows to appreciate the interaction features built inside?
    Thanks!

    OK, got it. Thank you!
    2015-01-17 14:46 GMT+01:00 Bob Levine <[email protected]>:
        How to share a project with a non-Adobe user and present it to
    prospect customers?  created by Bob Levine
    <https://forums.adobe.com/people/BobLevine> in Digital Publishing Suite
    - View the full discussion
    <https://forums.adobe.com/message/7105349#7105349>

  • HT5622 I own three Apple devices, but my iPad remained associated with my old email address and password. How can I register it with the same new email and password?

    I own three Apple devices, but my iPad remained associated with my old email address and password. How can I register it with the same new email and password?

    If you updated your existing account then try logging out of it on the iPad by tapping on the id in Settings > Store and then log back in and see if that 'refreshes' the account on the iPad. If you created a new account then any content that you purchased/downloaded via the old account will remain tied to that old account, and only that old account can download updates to its apps.

  • How can I tell if Adobe Flash is installed properly and upto date version?

    How can I tell if Adobe Flash is installed properly and upto date version?
    Hi Guys and girls, hopefully an easy question for you.
    I installed Adobe flash and lightsource last night, cos I needed it to run Weebly website maker.
    Anyone know if Weebly runs correctly on 10.9.1??
    I have a new comp, so no previous version of flash on here, I am running OSX 10.9.1
    I just want to know How I can tell if Adobe Flash is installed properly and is the upto date version?
    Where do I go in the comp to find out,
    If I type in Adobe flash into search it only brings up flash manager, when I click this it is an uninstaller program.
    Lightsource shows in finder apps.
    If I type in flash player it shows up in plugins with a version number see screen shot.
    Where would I go to check for updates for things like flash on my comp?
    Thanks Team,
    Jason in oz.   :?):)  

    1. Safari > Preferences > Security > Internet Plug-ins
        Allow Plug-ins
    2. Go to this Adobe site to check. You will see the animation above the tree.
    How to know whether AdobeFlash Player is installed
    http://helpx.adobe.com/flash-player.html?promoid=ISMRY

  • How to identify the SQLs which are using the tables and new columns

    Hi
    I m using oracle 10G Database in windows. Developers have added some columns in some of the database tables and were asking to check whether there is some impact on performance or not. I have not done this performance tuning before. Kindly help me how to proceed further.
    How to obtain the sqls which are touching the tables and the new columns? It would be really great if you can help me with this.
    Thanks

    You can try to use DBA_DEPENDENCIES to get PL/SQL objects using tables: http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_1041.htm#i1576452.
    However if SQL code is not stored in database in a trigger, a procedure, a function, a package or a view, it is impossible to retrieve all SQL code referencing some table from database dictionary: for this you would have to analyze application source code.

  • How to store ,retreive and edit  data from an xml file

    I am new to XML. I am creating a phone book in JSP with backend as an XML file.How can I store retrieve and edit datas from a jsp file.
    Please provide me with examples.I dont want to use bean.
    Please provide examples

    You will have to do some leg-work yourself. You will probably want to use DOM to parse and manipulate your XML data. Xerces (xml.apache.org) is a good open source parser. There are extensive examples on their site.
    - Saish

  • How to get ALV Output with selected Layout

    I have a program which displays the output in ALV list format and i changed the layout and saved
    In the selection-screen i have a field to select the layout after selecting the layout it output is not coming with that layout
    can anyone tell me what is the problem
    or give me a sample code
    Thank you

    Hi,
    Check this code.
    Hope it helps.
    *& Report  zSALES_ORDER
    report  z_sales_order.
    type-pools: slis.
    *DATA DECLARATION.
    types: begin of i_vbak,
           vbeln type vbeln_va,
           augru type augru,
           erdat type erdat,
           end of i_vbak.
    types: begin of i_vbap,
           vbeln type vbeln_va,
           augru type augru,
           posnr type posnr_va,
           matnr type matnr,
           zmeng type dzmeng,
           end of i_vbap.
    types: begin of i_makt,
           matnr type matnr,
           maktx type maktx,
           end of i_makt.
    types: begin of i_lips,
           posnr type posnr_vl,
           matnr type matnr,
           lfimg type lfimg,
           end of i_lips.
    types: begin of i_reason,
           spras type spras,
           augru type augru,
           bezei type bezei40,
           end of i_reason.
    types: begin of i_vbpa,
           vbeln type vbeln,
           parvw type parvw,
           kunnr type kunnr,
           end of i_vbpa.
    types: begin of i_kna1,
           kunnr type kunnr,
           name1 type name1_gp,
           end of i_kna1.
    types: begin of i_final,
           vbeln type vbeln_va,
           posnr type posnr_va,
           matnr type matnr,
           zmeng type dzmeng,
           maktx type maktx,
           lfimg type lfimg,
           spras type spras,
           augru type augru,
           bezei type bezei40,
           parvw type parvw,
           kunnr type kunnr,
           name1 type name1_gp,
           end of i_final.
    *INTERNAL TABLES
    data: it_vbak type standard table of i_vbak.
    data: it_vbap type standard table of i_vbap.
    data: it_makt type standard table of i_makt.
    data: it_lips type standard table of i_lips.
    data: it_vbpa type standard table of i_vbpa.
    data: it_kna1 type standard table of i_kna1.
    data: it_final type standard table of i_final.
    data: it_reason type standard table of i_reason.
    data : v_flag.
    *WORK AREA
    data: wa_vbak type i_vbak.
    data: wa_vbap type i_vbap.
    data: wa_makt type i_makt.
    data: wa_lips type i_lips.
    data: wa_vbpa type i_vbpa.
    data: wa_kna1 type i_kna1.
    data: wa_final type i_final.
    data: wa_reason type i_reason.
    *DATA DECLARATION.
    data: v_progname type sy-repid.
    data: v_vbeln type vbak-vbeln.
    data: v_gridtitle type lvc_title.
    *PARAMETERS: D_VARI LIKE DISVARIANT-VARIANT.
    data: i_variant like disvariant.
    data: p_vari like disvariant-variant.
    *DATA DECLARATION FOR CATALOGS LAYOUT SORT EVENTCATALOG TOPOFPAGE.
    data: i_fieldcat type slis_t_fieldcat_alv.
    data: i_layout type slis_layout_alv.
    data: i_sortinfo type slis_t_sortinfo_alv.
    data: i_listheader type slis_t_listheader.
    data: i_eventcat type slis_t_event.
    *INITIALIZATION - First point of execution of program
                    To initialize Any variables that are to be
                    used in the program, even before selection screen
                    appears.
    initialization.
      v_progname = sy-repid.
      v_flag = space.
      perform z_default_variant.
    *SELECTION SCREEN
      selection-screen begin of block blk1 with frame title text-002.
      select-options: s_vbeln for v_vbeln.
      selection-screen end of block blk1.
    *Variable for ALV Variant
      selection-screen begin of block b_var with frame title text-020.
      parameters: d_vari like disvariant-variant.
      selection-screen end of block b_var.
    *AT SLECTION-SCREEN.
    at selection-screen.
      perform z_validations.
    *AT SELECTION SCREEN.
    at selection-screen on d_vari.
    CHECK FOR THE EXISTENCE OF THE VARIANT SELECTED
      perform zf_check_var_exist.
    at selection-screen on value-request for d_vari.
    PROVIDE THE F4-HELP.
      perform zf_variant_f4.
    *START OF SELECTION.
    start-of-selection.
      perform z_select.
      perform check_validation_flag.
      perform z_fieldcat using i_fieldcat.
      perform z_layout.
      perform z_sortinfo using i_sortinfo.
      perform z_eventcat using i_eventcat.
      perform z_gridtitle.
      perform z_listheader using i_listheader.
      perform z_display.
    *&      Form  Z_VALIDATIONS
          text
    -->  p1        text
    <--  p2        text
    form z_validations .
      select vbeln into v_vbeln
                   up to 1 rows
                   from vbak
                   where vbeln in s_vbeln.
      endselect.
      if sy-subrc <> 0.
        message i002(sy) with 'No Records'.
        v_flag = 'X'.
      endif.
    endform.                    " Z_VALIDATIONS
    *&      Form  Z_FIELDCAT
          text
         -->P_I_FIELDCAT  text
    form z_fieldcat  using  p_i_fieldcat type slis_t_fieldcat_alv.
      data: i_fieldcat type slis_fieldcat_alv.
    *VBAK-VELN
      i_fieldcat-col_pos     = '1'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'VBELN'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-key         = 'X'.                        " SO THAT THIS FIELD IS NOT SCROLLABLE AND HIDDABLE.
      i_fieldcat-just        = 'C'.                        " FOR JUSTIFICATION.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'SALES ORDER'.              " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   =  15.                         " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBAK'.                     " FOR F1 & F4 HELP AS REFERNCED TO THE DDIC TABLE.
      append i_fieldcat to p_i_fieldcat.
    *VBAP-SPRAS
      i_fieldcat-col_pos     = '2'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'SPRAS'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'LANGUAGE'.                     " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 5.                          " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'TVAUT'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *VBAP-AUGRU
      i_fieldcat-col_pos     = '3'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'AUGRU'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'REASON'.                     " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 5.                          " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBAK'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *VBAP-bezei
      i_fieldcat-col_pos     = '4'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'BEZEI'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'DESCRIPTION'.                     " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 20.                          " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'TVAUT'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *VBAP-POSNR
      i_fieldcat-col_pos     = '5'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'POSNR'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'ITEM'.                     " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 8.                          " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBAP'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *VBAP-MATNR.
      i_fieldcat-col_pos     = '6'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'MATNR'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'MATERIAL'.                 " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 10.                         " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBAP'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *VBAP-ZMENG
      i_fieldcat-col_pos     = '7'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'ZMENG'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'QUANT'.                     " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 10.                          " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBAP'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *MAKT-MAKTX
      i_fieldcat-col_pos     = '8'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'MAKTX'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'DESCRIPTION'.                     " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 20.                          " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'MAKT'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *LIPS-VBELN
      i_fieldcat-col_pos     = '9'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'VBELN'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'DELIVERY'.                 " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   = 15.                         " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'LIPS'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *LIPS-LFIMG
      i_fieldcat-col_pos     = '10'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'LFIMG'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-do_sum      = 'X'.
      i_fieldcat-seltext_l   = 'LFIMG'.                    " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   =  18.                        " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'LIPS'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *parvw
      i_fieldcat-col_pos     = '11'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'PARVW'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'PARTNER FUN'.                    " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   =  18.                        " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBPA'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *KUNNR
      i_fieldcat-col_pos     = '12'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'KUNNR'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'SHIP'.                    " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   =  18.                        " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'VBPA'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    *NAME1
      i_fieldcat-col_pos     = '13'.                        " POSITION OF THE COLUMN.
      i_fieldcat-fieldname   = 'NAME1'.                    " FIELD FOR WHICH CATALOG ID FILLED.
      i_fieldcat-tabname     = 'IT_FINAL'.                 " INTERNAL TABLE TO WHICH THE FIELD BELONGS TO.
      i_fieldcat-lzero       = 'X'.                        " OUTPUT WITH LEADING ZEROS.
      i_fieldcat-seltext_l   = 'NAME'.                    " LONG TEXT FOR HEADER.
      i_fieldcat-outputlen   =  18.                        " SET THE OUTPUT LENGTH.
      i_fieldcat-ref_tabname = 'KNA1'.                     " FOR F1 & F4 HELP AS REFERNCED TO TABLE.
      append i_fieldcat to p_i_fieldcat.
    endform.                    " Z_FIELDCAT
    *&      Form  Z_SELECT
          text
    -->  p1        text
    <--  p2        text
    form z_select .
      select vbeln
             augru erdat into corresponding fields of table it_vbak
                   from vbak
                   where vbeln in s_vbeln.
      loop at it_vbak into wa_vbak.
        wa_vbak-erdat = '99991231'.
        modify it_vbak from wa_vbak transporting erdat.
      endloop.
    select * from vbak into corresponding fields of table it_vbak
                   for all entries in it_vbak
                   where erdat = it_vbak-erdat.
      if sy-subrc = 0.
        select vbeln
               posnr
               matnr
               zmeng into corresponding fields of table it_vbap
                     from vbap
                     for all entries in it_vbak
                     where vbeln = it_vbak-vbeln.
        if sy-subrc = 0.
          select spras
                 augru
                 bezei into corresponding fields of table it_reason
                       from tvaut
                       for all entries in it_vbak
                       where augru = it_vbak-augru.
          if sy-subrc = 0.
            select vbeln
                   parvw
                   kunnr into corresponding fields of table it_vbpa
                         from vbpa
                         for all entries in it_vbak
                         where vbeln = it_vbak-vbeln.
            if sy-subrc = 0.
              select kunnr
                     name1 into corresponding fields of table it_kna1
                           from kna1
                           for all entries in it_vbpa
                           where kunnr = it_vbpa-kunnr.
              if sy-subrc = 0.
                select posnr
                       matnr
                       lfimg into corresponding fields of table it_lips
                             from lips
                             for all entries in it_vbap
                             where posnr = it_vbap-posnr.
                if sy-subrc = 0.
                  select matnr
                         maktx into corresponding fields of table it_makt
                               from makt
                               for all entries in it_vbap
                               where matnr = it_vbap-matnr.
                endif.
              endif.
            endif.
          endif.
        endif.
      else.
        message i002(sy) with 'No Data found'.
        leave list-processing.
      endif.
      if sy-subrc = 0.
        loop at it_vbap into wa_vbap.
          read table it_vbak into wa_vbak with key vbeln = wa_vbap-vbeln binary search.
          if sy-subrc = 0.
    *MOVE DATA INTO IT_FINAL.
            move: wa_vbak-vbeln to wa_final-vbeln,
                  wa_vbak-augru to wa_final-augru,
                  wa_vbap-posnr to wa_final-posnr,
                  wa_vbap-matnr to wa_final-matnr,
                  wa_vbap-zmeng to wa_final-zmeng.
          endif.
          read table it_lips into wa_lips with key posnr = wa_final-posnr binary search.
          if sy-subrc = 0.
            move: wa_lips-lfimg to wa_final-lfimg.
          endif.
          read table it_reason into wa_reason with key augru = wa_final-augru binary search.
          if sy-subrc = 0.
            move: wa_reason-spras to wa_final-spras,
                  wa_reason-bezei to wa_final-bezei.
          endif.
          read table it_makt into wa_makt with key matnr = wa_final-matnr binary search.
          if sy-subrc = 0.
            move: wa_makt-maktx to wa_final-maktx.
          endif.
         read table it_vbpa into wa_vbpa with key vbeln = wa_final-vbeln binary search.
          if sy-subrc = 0.
            move: wa_vbpa-parvw to wa_final-parvw,
                  wa_vbpa-kunnr to wa_final-kunnr.
          endif.
           read table it_kna1 into wa_kna1 with key kunnr = wa_final-kunnr binary search.
          if sy-subrc = 0.
            move: wa_kna1-name1 to wa_final-name1.
          endif.
          append wa_final to it_final.
          clear wa_final.
        endloop.
      endif.
    endform.                    " Z_SELECT
    *&      Form  Z_LAYOUT
          text
    -->  p1        text
    <--  p2        text
    form z_layout .
      i_layout-zebra = 'X'.
      i_layout-totals_text = 'Total'(a00).
      i_layout-subtotals_text = 'SubTotal'(a01).
      i_layout-box_tabname = 'IT_FINAL'.
    endform.                    " Z_LAYOUT
    *&      Form  Z_SORTINFO
          text
         -->P_I_SORTINFO  text
    form z_sortinfo  using    p_i_sortinfo type slis_t_sortinfo_alv.
      data: i_sortinfo type slis_sortinfo_alv.
      clear i_sortinfo.
      i_sortinfo-spos = '1'.
      i_sortinfo-fieldname = 'VBELN'.
      i_sortinfo-tabname = 'IT_FINAL'.
      i_sortinfo-up = 'X'.
      i_sortinfo-group = 'UL'.                     " I.E UNDERLINE AFTER EVERY GROUP
      i_sortinfo-subtot = 'X'.
      append i_sortinfo  to p_i_sortinfo.
    endform.                    " Z_SORTINFO
    *&      Form  Z_EVENTCAT
          text
         -->P_I_EVENTCAT  text
    form z_eventcat  using    p_i_eventcat type slis_t_event.
      data: i_event type slis_alv_event.
      call function 'REUSE_ALV_EVENTS_GET'
        exporting
          i_list_type     = 0
        importing
          et_events       = p_i_eventcat
        exceptions
          list_type_wrong = 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.
      clear i_event.
      read table p_i_eventcat with key name = slis_ev_top_of_page into
      i_event.
      if sy-subrc = 0.
        move 'TOP_OF_PAGE' to i_event-form.
        append i_event to p_i_eventcat.
      endif.
      read table p_i_eventcat with key name = slis_ev_pf_status_set into i_event.
      if sy-subrc = 0.
        move 'SET_PF_STATUS' to i_event-form.
        append i_event to p_i_eventcat.
      endif.
      clear i_event.
      read table p_i_eventcat into i_event with key name = slis_ev_user_command .
      if sy-subrc = 0.
        move 'USER_COMMAND' to i_event-form.
        append i_event to p_i_eventcat.
      endif.
    endform.                    " Z_EVENTCAT
    *&      Form  Z_DISPLAY
          text
    -->  p1        text
    <--  p2        text
    form z_display .
      call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
         i_callback_program                = v_progname
         i_callback_pf_status_set          = 'SET_PF_STATUS'
         i_callback_user_command           = 'USER_COMMAND'
         i_callback_top_of_page            = 'TOP_OF_PAGE'
         i_grid_title                      = v_gridtitle
         i_save                            = 'A'
         is_layout                         = i_layout
         it_fieldcat                       = i_fieldcat[]
         it_sort                           = i_sortinfo
         it_events                         = i_eventcat
    IMPORTING
      E_EXIT_CAUSED_BY_CALLER           =
      ES_EXIT_CAUSED_BY_USER            =
        tables
          t_outtab                          = it_final
       exceptions
         program_error                     = 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.
    endform.                    " Z_DISPLAY
    *&      Form  Z_GRIDTITLE
          text
    -->  p1        text
    <--  p2        text
    form z_gridtitle .
      v_gridtitle = 'ALV FOR SALES ORDER DISPLAY'.
    endform.                    " Z_GRIDTITLE
    *TOP OF PAGE.
    form top_of_page.
      call function 'REUSE_ALV_COMMENTARY_WRITE'
        exporting
          it_list_commentary = i_listheader.
    endform.                    "TOP_OF_PAGE
    *MENU SETTINGS.
    form set_pf_status using rt_extab type slis_t_extab.
      set pf-status 'ALV_MENU'.
    endform.                    "SET_PF_STATUS
    *USER-COMMAND
    form user_command using p_ucomm type sy-ucomm
                            rs_selfield type slis_selfield.
      data : vbeln type vbeln_va.
      case p_ucomm.
        when 'BACK'.
          leave program.
        when '&IC1'.
          clear : vbeln.
          vbeln = rs_selfield-value.
          set parameter id: 'AUN' field vbeln.
          call transaction 'VA03' and skip first screen.
      endcase.
    endform.                    " USER_COMMAND
    *&      Form  Z_LISTHEADER
          text
         -->P_I_LISTHEADER  text
    form z_listheader using p_i_listheader type slis_t_listheader.
      data: l_listheader type slis_listheader.
      refresh p_i_listheader.
      clear l_listheader.
      l_listheader-typ = 'H'.
      l_listheader-info = text-001.
      append l_listheader to p_i_listheader.
      clear l_listheader.
      l_listheader-typ = 'H'.
      l_listheader-info = text-002.
      append l_listheader to p_i_listheader.
    endform.                    " Z_LISTHEADER
    *&      Form  check_validation_flag
          text
    -->  p1        text
    <--  p2        text
    form check_validation_flag .
      if not v_flag is initial.
        leave list-processing.
      endif.
    endform.                    " check_validation_flag
    *&      Form  Z_DEFAULT_VARIANT
          text
    -->  p1        text
    <--  p2        text
    form z_default_variant .
      i_variant-report = v_progname.
      call function 'REUSE_ALV_VARIANT_DEFAULT_GET'
        exporting
          i_save        = 'A'
        changing
          cs_variant    = i_variant
        exceptions
          wrong_input   = 1
          not_found     = 2
          program_error = 3
          others        = 4.
      if sy-subrc = 0.
        p_vari = i_variant-variant.
       D_VARI = P_VARI.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      endif.
    endform.                    " Z_DEFAULT_VARIANT
    *&      Form  ZF_CHECK_VAR_EXIST
          text
    -->  p1        text
    <--  p2        text
    form zf_check_var_exist .
      data: l_variant like disvariant.
      if not p_vari is initial.
        clear l_variant.
        l_variant-report = v_progname.
        l_variant-variant = p_vari.
        call function 'REUSE_ALV_VARIANT_EXISTENCE'
          exporting
            i_save        = 'U'
          changing
            cs_variant    = l_variant
          exceptions
            wrong_input   = 1
            not_found     = 2
            program_error = 3
            others        = 4.
        if sy-subrc = 0.
          clear i_variant.
          move: l_variant-variant to i_variant-variant,
                l_variant-report to i_variant-report.
        else.
          message id sy-msgid type sy-msgty number sy-msgno
             with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        endif.
      endif.
    endform.                    " ZF_CHECK_VAR_EXIST
    *&      Form  ZF_VARIANT_F4
          text
    -->  p1        text
    <--  p2        text
    form zf_variant_f4 .
      data: x_variant like disvariant.
      call function 'REUSE_ALV_VARIANT_F4'
        exporting
          is_variant                = i_variant
      I_TABNAME_HEADER          =
      I_TABNAME_ITEM            =
      IT_DEFAULT_FIELDCAT       =
         i_save                    = 'U'
         i_display_via_grid        = 'X'
       importing
      E_EXIT                    =
         es_variant                = x_variant
       exceptions
         not_found                 = 1
         program_error             = 2
         others                    = 3
      if sy-subrc = 0.
        p_vari = x_variant-variant.
        d_vari = p_vari.
        clear i_variant.
        move: x_variant-variant to i_variant-variant,
              x_variant-report to i_variant-report.
      else.
        message id sy-msgid type sy-msgty number sy-msgno
                with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      endif.
    endform.                    " ZF_VARIANT_F4
    Regards,
    Pritha.

  • How to do the calculations with currency fields in table control?

    Hi everybody,
    Can anyone tell me how to do the calculations (arithmetical) with the currency fields which have been assigned for a table control fields? Actually they should be fetched from the database table and need to do some calculations and after that the same should get updated at the database level.
    Here, i am getting the short dump after doing the calculations and trying to display at the table control field itself.....
    Can anyone help me in this issue........
    Thank you very much.....in advance,
    Somu.

    Hi,
    Thanks to your replies all,
    But, even though the sign check box is checked in the SAP domain WRTV7, in my program it is not showing the negative sign at all...
    I am keep trying for all the options...
    But still it is not working out...
    My requirement is after fetching the data from the database i need to do calculations and save back same to the customized table field, for which the domain i have checked the sign and have done it also...
    I'd be highly thankful to you, if you can help me out...
    Thank you,
    Somu.

  • ALV report with dynamic columns, and repeated structure rows

    Hey Guys,
    I've done some ALV programming, but most of the reports were straight forward. This one is a little interesting. So here go the questions...
    Q1: Regarding Columns:
    What is the best way to code a report with columns being dynamic. This is one of the parameters the user is going to enter in his input.
    Q2: Regarding Rows:
    I want to repeat a structure(say it contains f1, f2, f3) multiple time in rows. What is the best way to do it? The labels for these fields have to appear in the first column.
    Below is the visual representation of the questions.
    Jan 06  , Feb 06, Mar 06....(dynamic)
       material 1
    Current Stock
    current required
    $Value of stock
       material 2
    Current Stock
    current required
    $Value of stock
       material 3
    Current Stock
    current required
    $Value of stock
    Thanks for your help.
    Sumit.

    Hi Sumit,
    Just check this sample from one of the SAP site
    ABAP Code Sample for Dynamic Table for ALV with Cell Coloring
    Applies To:
    ABAP / ALV Grid
    Article Summary
    ABAP Code Sample that uses dynamic programming techniques to build a dynamic internal table for display in an ALV Grid with Cell Coloring.
    Code Sample
    REPORT zcdf_dynamic_table.
    * Dynamic ALV Grid with Cell Coloring.
    * Build a field catalog dynamically and provide the ability to color
    * the cells.
    * To test, copy this code to any program name and create screen 100
    * as described in the comments. After the screen is displayed, hit
    * enter to exit the screen.
    * Tested in 4.6C and 6.20
    * Charles Folwell - [email protected] - Feb 2, 2005
    DATA:
    r_dyn_table TYPE REF TO data,
    r_wa_dyn_table TYPE REF TO data,
    r_dock_ctnr TYPE REF TO cl_gui_docking_container,
    r_alv_grid TYPE REF TO cl_gui_alv_grid,
    t_fieldcat1 TYPE lvc_t_fcat, "with cell color
    t_fieldcat2 TYPE lvc_t_fcat, "without cell color
    wa_fieldcat LIKE LINE OF t_fieldcat1,
    wa_cellcolors TYPE LINE OF lvc_t_scol,
    wa_is_layout TYPE lvc_s_layo.
    FIELD-SYMBOLS:
    <t_dyn_table> TYPE STANDARD TABLE,
    <wa_dyn_table> TYPE ANY,
    <t_cellcolors> TYPE lvc_t_scol,
    <w_field> TYPE ANY.
    START-OF-SELECTION.
    * Build field catalog based on your criteria.
    wa_fieldcat-fieldname = 'FIELD1'.
    wa_fieldcat-inttype = 'C'.
    wa_fieldcat-outputlen = '10'.
    wa_fieldcat-coltext = 'My Field 1'.
    wa_fieldcat-seltext = wa_fieldcat-coltext.
    APPEND wa_fieldcat TO t_fieldcat1.
    wa_fieldcat-fieldname = 'FIELD2'.
    wa_fieldcat-inttype = 'C'.
    wa_fieldcat-outputlen = '10'.
    wa_fieldcat-coltext = 'My Field 2'.
    wa_fieldcat-seltext = wa_fieldcat-coltext.
    APPEND wa_fieldcat TO t_fieldcat1.
    * Before adding cell color table, save fieldcatalog to pass
    * to ALV call. The ALV call needs a fieldcatalog without
    * the internal table for cell coloring.
    t_fieldcat2[] = t_fieldcat1[].
    * Add cell color table.
    * CALENDAR_TYPE is a structure in the dictionary with a
    * field called COLTAB of type LVC_T_SCOL. You can use
    * any structure and field that has the type LVC_T_SCOL.
    wa_fieldcat-fieldname = 'T_CELLCOLORS'.
    wa_fieldcat-ref_field = 'COLTAB'.
    wa_fieldcat-ref_table = 'CALENDAR_TYPE'.
    APPEND wa_fieldcat TO t_fieldcat1.
    * Create dynamic table including the internal table
    * for cell coloring.
    CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
    it_fieldcatalog = t_fieldcat1
    IMPORTING
    ep_table = r_dyn_table
    EXCEPTIONS
    generate_subpool_dir_full = 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.
    * Get access to new table using field symbol.
    ASSIGN r_dyn_table->* TO <t_dyn_table>.
    * Create work area for new table.
    CREATE DATA r_wa_dyn_table LIKE LINE OF <t_dyn_table>.
    * Get access to new work area using field symbol.
    ASSIGN r_wa_dyn_table->* TO <wa_dyn_table>.
    * Get data into table from somewhere. Field names are
    * known at this point because field catalog is already
    * built. Read field names from the field catalog or use
    * COMPONENT <number> in a DO loop to access the fields. A
    * simpler hard coded approach is used here.
    ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.
    <w_field> = 'ABC'.
    ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.
    <w_field> = 'XYZ'.
    APPEND <wa_dyn_table> TO <t_dyn_table>.
    ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.
    <w_field> = 'TUV'.
    ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.
    <w_field> = 'DEF'.
    APPEND <wa_dyn_table> TO <t_dyn_table>.
    * Color cells based on your criteria. In this example, a test on
    * FIELD2 is used to decide on color.
    LOOP AT <t_dyn_table> INTO <wa_dyn_table>.
    ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.
    * Get access to internal table used to color cells.
    ASSIGN COMPONENT 'T_CELLCOLORS'
    OF STRUCTURE <wa_dyn_table> TO <t_cellcolors>.
    CLEAR wa_cellcolors.
    wa_cellcolors-fname = 'FIELD2'.
    IF <w_field> = 'DEF'.
    wa_cellcolors-color-col = '7'.
    ELSE.
    wa_cellcolors-color-col = '5'.
    ENDIF.
    APPEND wa_cellcolors TO <t_cellcolors>.
    MODIFY <t_dyn_table> FROM <wa_dyn_table>.
    ENDLOOP.
    * Display screen. Define screen 100 as empty, with next screen
    * set to 0 and flow logic of:
    * PROCESS BEFORE OUTPUT.
    * MODULE initialization.
    * PROCESS AFTER INPUT.
    CALL SCREEN 100.
    * MODULE initialization OUTPUT
    MODULE initialization OUTPUT.
    * Set up for ALV display.
    IF r_dock_ctnr IS INITIAL.
    CREATE OBJECT r_dock_ctnr
    EXPORTING
    side = cl_gui_docking_container=>dock_at_left
    ratio = '90'.
    CREATE OBJECT r_alv_grid
    EXPORTING i_parent = r_dock_ctnr.
    * Set ALV controls for cell coloring table.
    wa_is_layout-ctab_fname = 'T_CELLCOLORS'.
    * Display.
    CALL METHOD r_alv_grid->set_table_for_first_display
    EXPORTING
    is_layout = wa_is_layout
    CHANGING
    it_outtab = <t_dyn_table>
    it_fieldcatalog = t_fieldcat2.
    ELSE. "grid already prepared
    * Refresh display.
    CALL METHOD r_alv_grid->refresh_table_display
    EXPORTING
    i_soft_refresh = ' '
    EXCEPTIONS
    finished = 1
    OTHERS = 2.
    ENDIF.
    ENDMODULE. " initialization OUTPUT
    Regards
    vijay

  • Manipulate Layout on ALV Grid with dynamic table

    Dear all
    i'm generating a dynamic table depending of a date selection. That means that I show columns for weeks and the quantity of weeky migh change.
    Now the users wants to have a specific layout of the ALV grid with totals. When he saves the layout, only the weeks at this selection will show the next time he runs the programm with a larger selection.
    a) Is it possible to modify the layout during runtime by programming?
    b) Do you have any other ideas how to solve this problem?
    Thank you

    You don't know the names of your columns? hmm you do, because before you created dynamic table you had to create field catalog, so the structure and column names of newly (dynamically) created table will be the same like defined in the field catalog.
    The last loop also does not look good, in my opinion should be something like:
    LOOP AT lt_datatable +(my first table)+ ASSIGNING <ls_data4>.
        AT NEW pernr.
          APPEND initial line to <fs_1> assigning <fs_2>.
          <fs_2>-pernr = <ls_data4>-pernr.
        ENDAT.
        ASSIGN COMPONENT <ls_data4>-wage_type OF STRUCTURE <fs_2> TO <fs_5>.
        <fs_5> = <ls_data4>-amount.
    ENDLOOP.
    also keep in mind that number of calls of method cl_alv_table_create=>create_dynamic_table is limited to 36 (?) calls within one program session because it uses dynamic subroutine pool behind so you will have short dump if you will execute that 37 times.

  • How to save a variant with dynamic selections parameters

    Anybody knows how to save a variant for an ABAP that uses a Logical database with Dynamic Selections?
    Have a look for example to the following:
    SE38 - DEMO_PROGRAM_GET - Execute - Shift F4 - Connection Number.
    How to save 0820 as Connection Number?
    Function Group SVAR seems good but FREE SELECTIONs are not easy to manage...

    Hello,
      I tried to save the variant of DEMO_PROGRAM_GET with dynamic selection field (Connection Number) filled. It gets saved without any problem. Just click 'SAVE' and enter the variant name and description.
    Thanks,
    Venu

Maybe you are looking for