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 REPORT

Former Member
0 Likes
761

Is it possible to calculate a formula in subtotal line of ALV GRID DISPLAY.

For eq I have 4 columns - GSBER, Sale Qty, Sale Value, Sale Per Ton.

When I'll take subtotal on GSBER, total of QTY and sale value will be calulated automatically , but I want to calculate Sales Per ton based on Total Sale Value and Total Sales Qty. Is it possible?

Regards,

Sachin Raut

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
726

Hi,

Yes it is possible....


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program     = l_repid
*      i_callback_top_of_page = 'TOP_OF_PAGE'
      is_layout              = l_layout
      is_print               = l_print
      it_sort                = it_sort
      it_filter              = it_filter
      it_fieldcat            = t_fieldcat[]
      it_events              = t_event
      it_event_exit          = it_event_exit
    TABLES
      t_outtab               = <tab>
    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.

*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM top_of_page.

  DATA: lo_grid TYPE REF TO cl_gui_alv_grid.

* get the global reference
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_grid.

* get the subtotal
  DATA: it_01 TYPE REF TO data.

  CALL METHOD lo_grid->get_subtotals
    IMPORTING
      ep_collect01 = it_01.

* change the data
  FIELD-SYMBOLS: <ft_tab> TYPE ANY TABLE,
                 <fs_tab> TYPE ANY,
                 <fs_value1> TYPE ANY,
                 <fs_value2> TYPE ANY,
                 <ff_field> TYPE ANY.
  ASSIGN it_01->* TO <ft_tab>.


  LOOP AT <ft_tab> ASSIGNING <fs_tab>.
    ASSIGN COMPONENT 'NETPR' OF STRUCTURE <fs_tab> TO <ff_field>.
    ASSIGN COMPONENT 'KWMENG' OF STRUCTURE <fs_tab> TO <fs_value1>.
    ASSIGN COMPONENT 'NETWR' OF STRUCTURE <fs_tab> TO <fs_value2>.
    <ff_field> = <fs_value2> / <fs_value1> .
  ENDLOOP.

* Refresh the table display
  CALL METHOD lo_grid->refresh_table_display
    EXPORTING
      i_soft_refresh = 'X'.


ENDFORM.                    " top_of_page

Is it possible to calculate a formula in subtotal line of ALV GRID DISPLAY.

For eq I have 4 columns - GSBER, Sale Qty, Sale Value, Sale Per Ton.

When I'll take subtotal on GSBER, total of QTY and sale value will be calulated automatically , but I want to calculate Sales Per ton based on Total Sale Value and Total Sales Qty. Is it possible?

Regards,

Sachin Raut

3 REPLIES 3
Read only

Former Member
0 Likes
727

Hi,

Yes it is possible....


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program     = l_repid
*      i_callback_top_of_page = 'TOP_OF_PAGE'
      is_layout              = l_layout
      is_print               = l_print
      it_sort                = it_sort
      it_filter              = it_filter
      it_fieldcat            = t_fieldcat[]
      it_events              = t_event
      it_event_exit          = it_event_exit
    TABLES
      t_outtab               = <tab>
    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.

*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM top_of_page.

  DATA: lo_grid TYPE REF TO cl_gui_alv_grid.

* get the global reference
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_grid.

* get the subtotal
  DATA: it_01 TYPE REF TO data.

  CALL METHOD lo_grid->get_subtotals
    IMPORTING
      ep_collect01 = it_01.

* change the data
  FIELD-SYMBOLS: <ft_tab> TYPE ANY TABLE,
                 <fs_tab> TYPE ANY,
                 <fs_value1> TYPE ANY,
                 <fs_value2> TYPE ANY,
                 <ff_field> TYPE ANY.
  ASSIGN it_01->* TO <ft_tab>.


  LOOP AT <ft_tab> ASSIGNING <fs_tab>.
    ASSIGN COMPONENT 'NETPR' OF STRUCTURE <fs_tab> TO <ff_field>.
    ASSIGN COMPONENT 'KWMENG' OF STRUCTURE <fs_tab> TO <fs_value1>.
    ASSIGN COMPONENT 'NETWR' OF STRUCTURE <fs_tab> TO <fs_value2>.
    <ff_field> = <fs_value2> / <fs_value1> .
  ENDLOOP.

* Refresh the table display
  CALL METHOD lo_grid->refresh_table_display
    EXPORTING
      i_soft_refresh = 'X'.


ENDFORM.                    " top_of_page

Read only

0 Likes
726

Thank you very much, my problem is solved, but a new problem is created. While displaying the same output in print preview the calculated figures are not shown, it is showing the total of that column which is wrong as i am calulating per ton . Do you have any solution for this.

Regards,

Sachin Raut

Read only

0 Likes
726

guys am having a problem with my program when I run it in Backround for this. The ASSIGN it_01->* TO <ft_tab>.  does not work in backround it only works in foreground. How can I solve this