Application Development 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: 

How to Auto Increase number in ALV Grid Fieldcat?

former_member820995
Discoverer
0 Kudos
How to auto increase the number list in col_pos 1? I tried put ls_fcat-auto_value like the similar post but not working. It said that the error in ls_fcat-fieldname whenever I deleted or not. Please help!Thanks.
clear: ls_fcat.
  
ls_fcat-reptext = 'A'.
ls_fcat-coltext = 'A'.
ls_fcat-fieldname = 'VBELN'.
ls_fcat-AUTO_VALUE = 'X'.
ls_fcat-outputlen = '15'.
ls_fcat-col_pos = 1.
append ls_fcat to fieldcat.
clear: ls_fcat.
ls_fcat-reptext = 'B'.
ls_fcat-coltext = 'B'.
ls_fcat-fieldname = 'VBELN'.
ls_fcat-ref_table = 'IALV'.
ls_fcat-hotspot = 'X'.
ls_fcat-outputlen = '15'.
ls_fcat-col_pos = 2.
append ls_fcat to fieldcat.
clear: ls_fcat.
ls_fcat-reptext = 'C'.
ls_fcat-coltext = 'C'.
ls_fcat-fieldname = 'POSNR'.
ls_fcat-ref_table = 'IALV'.
ls_fcat-outputlen = '10'.
ls_fcat-col_pos = 3.
append ls_fcat to fieldcat.
5 REPLIES 5

Nitish2027
Participant
0 Kudos

Hi tantrung69,

Define a variable lv_pos and set it as 1. From then on, keep increasing it by one. Something like below would help.

DATA: lv_pos(2) TYPE i.

clear: ls_fcat.

lv_pos = 1.
  
 ls_fcat-reptext = 'A'.
  
 ls_fcat-coltext = 'A'.
  
 ls_fcat-fieldname = 'VBELN'.
  
 ls_fcat-AUTO_VALUE = 'X'.
  
 ls_fcat-outputlen = '15'.
  
 ls_fcat-col_pos = lv_pos.
  
 append ls_fcat to fieldcat.
  
 clear: ls_fcat.

lv_pos = lv_pos + 1.
  
 ls_fcat-reptext = 'B'.
  
 ls_fcat-coltext = 'B'.
  
 ls_fcat-fieldname = 'VBELN'.
  
 ls_fcat-ref_table = 'IALV'.
  
 ls_fcat-hotspot = 'X'.
  
 ls_fcat-outputlen = '15'.
  
 ls_fcat-col_pos = lv_pos.
  
 append ls_fcat to fieldcat.
  
 clear: ls_fcat.

lv_pos = lv_pos + 1.
  
 ls_fcat-reptext = 'C'.
  
 ls_fcat-coltext = 'C'.
  
 ls_fcat-fieldname = 'POSNR'.
  
 ls_fcat-ref_table = 'IALV'.
  
 ls_fcat-outputlen = '10'.
  
 ls_fcat-col_pos = lv_pos.
  
 append ls_fcat to fieldcat.

It will be used more effectively if you are using it inside a loop on a itab which contains field catalog details and then you can use the variable lv_pos for position and keep increasing it by one in each iteration.

0 Kudos

Not working for me. I mean I want to make a column that auto increase number from 1->.. , not using fieldname. Anyway thanks!

matt
Active Contributor

I'll tell you how in one word: modularise.

DATA(column_position) = 0.
add_column_to_fcat( EXPORTING i_fcat = VALUE #( reptext = 'A'
                                                coltext = 'A'
                                                fieldname = 'VBELN'
                                                auto_value = abap_true
                                                output_len = 15
                      CHANGING x_field_cat       = fieldcat
                               x_column_position = column_position ).
...
similarly for the rest of your columns
METHODS add_column_to_fcat
  IMPORTING i_fcat TYPE ...
CHANGING x_field_cat TYPE ... x_column_position TYPE i.
METHOD add_column_to_fcat.
  x_column_position = x_column_position + 1.
  INSERT i_fcat INTO TABLE x_field_cat.
ENDMETHOD.

If you're on a pre 7.4 version of ABAP you can still do it, but need each field of the fcat that you're defining to be an input parameter, and you need a work area in the method code.

Another solution, which makes the thing reusable, would be to code the logic in its own Z_FCAT_BUILDER class.

Then your consumer code might look like:

DATA(fcat_builder) = new Z_FCAT_BUILDER( ).
fcat_builder->add_column( VALUE #( reptext = 'A'
                                   coltext = 'A'
                                   fieldname = 'VBELN'
                                   auto_value = abap_true
                                   output_len = 15 ) ).
fcat_builder->add_column...
... fieldcat = fcat_builder->get_fieldcat( ).

0 Kudos

I dont know making a column auto increase is that complicate.. anyway thanks.!

matt
Active Contributor
0 Kudos

In fact using a method or a seperate class is better simpler programming.