Skip to Content
Author's profile photo Former Member

Expand the list of columns in a report dynamically

We all know about ALV reports and the flexibility and user friendliness that ALV offers to users and ease of development for the technical consultants.

However, off late I have seen requirements where the users want dynamically add or remove columns depending on the data selected. For example, there is a report which compares the same set of fields for a given dimension. To be more precise if for a vendor / RFQ, you want to compare a list of fields and the user wants the comparison to be displayed vertically, that means more number of vendors / RFQ’s, more number of fields need to be added to the output.

Now to the field catalog you can add more fields at runtime depending on the data coming in. However, how to add more columns to the internal table that will be used to display the data on the ALV grid. One crude of way doing is declare an internal table big enough to handle maximum fields and depending on the actual fields coming in at runtime, build the field catalog only for those field required so that the extra fields will not be visible. However, this is not the efficient way doing the same and more over you have every chance that the code might short dump as the field catalog and the structure of the output internal table are in sync.

So the elegant solution for this is to create a table dynamically, with the list of the fields that we have determined at run time. The question is how do we do that. Here is a quick solution.

1. Create your field catalog either manually or automatically using the function module, LVC_FIELDCATALOG_MERGE. Add more rows to the field catalog table (T_FIELDCAT) at run time.

2. Use the field catalog to create a table dynamically using the method below.









Now the field symbol <T_OUTPUT> is pointing to an output table of the structure that contains the fields which were determined at runtime. Now fill this table with the data and pass <T_OUTPUT> to the method SET_TABLE_FOR_FIRST_DISPLAY and the ALV grid should show the data properly.

However, if you want to access or modify or fill in the data for specific columns, you have to use the ASSIGN COMPONENT statement for the specific field name and do it.

Assigned tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      Hi Ravi,

      Just a coincidence, but look at this weblog as well : /people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

      Subramanian V.

      Author's profile photo Former Member
      Former Member
      After all, unfortunately, looks like I was late.
      Author's profile photo Allen Thomas
      Allen Thomas
      how do i add a structure to dynamic table. for eg if i want to add lvc_t_styl to the dynamic table how do i define it.
      Author's profile photo José Gabriel Martínez
      José Gabriel Martínez
      Hi Ravikumar,

      I hope that besides this blog is very old, you could be still able to track it.

      I have a problem with a dynamic table and the event 'before_line_output'.

      Usually, I use this event to perform specific calculations for totals line (with REUSE_ALV_LIST_DISPLAY). I just write the desired code there and modify the respective columns of my outtab at the header line, checking:
      "if p_lineinfo-endsum EQ 'X' OR p_lineinfo-subtot EQ 'X'.".

      But, with a dynamic table ,there is a problem: there is no header line, so, I don't know how to "pass" my calculations to specific columns.

      I posted the issue at the forum, with tittle:
      "Dynamic itab (field-symbols) and ALV event BEFORE_LINE_OUTPUT".

      Is really important to keep the code the it is, I mean, with no mayo changes...but if I can not use the event before_line_output...then I would like to know if there is a method or function (may be LVC_TOTALS_GET?, I don't know) to do this.

      I have spent a lot of time looking foe demo codes in SAP or examples in internet, but it has been an unsuccesful search.

      Thanks in advance if you can read this "late" entry for your blog.

      José Gabriel Martínez.

      Author's profile photo Former Member
      Former Member
      Hi Ravikumar,

      how to set the heading dynamically in alv report.
      for eg. if i have 00001 production order which having operation 10,20, on then i want to display heading for this like oprn1 oprn2 oprn3 ....and so on.

      please reply.