Hi Sai Shankar,
If you want this functionality though it looks weird. Please use below code. I have written your logic in TOP_OF_PAGE .
*&---------------------------------------------------------------------*
*& Start of Selection Event *
*&---------------------------------------------------------------------*
start-of-selection.
perform alv_fieldcat.
perform get_events.
perform sub_comment_build using t_list_top_of_page.
perform alv_display.
*&---------------------------------------------------------------------*
*& Form ALV_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form alv_fieldcat .
data: counter type i.
counter = counter + 1.
wa_fieldcat-col_pos = counter.
wa_fieldcat-fieldname = 'FIELDNAME'.
wa_fieldcat-tabname = 'TABLENAME'.
wa_fieldcat-seltext_l = 'Label'.
wa_fieldcat-no_zero = 'X'.
append wa_fieldcat to it_fieldcat.
clear wa_fieldcat.
* DATA SORTING AND SUBTOTAL
data: gs_sort type slis_sortinfo_alv.
clear gs_sort.
gs_sort-fieldname = 'FIELD1'.
gs_sort-spos = 1.
gs_sort-up = 'X'.
gs_sort-subtot = 'X'.
append gs_sort to gt_sort.
endform. " ALV_FIELDCAT
*&---------------------------------------------------------------------*
*& Form GET_EVENTS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form get_events .
call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = it_events[]
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.
read table it_events with key name = slis_ev_top_of_page
into it_events.
if sy-subrc = 0.
move t_formname_top_of_page to it_events-form.
append it_events.
endif.
endform. " GET_EVENTS
*&---------------------------------------------------------------------*
*& Form SUB_COMMENT_BUILD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_T_LIST_TOP_OF_PAGE text
*----------------------------------------------------------------------*
form sub_comment_build using i_top_of_page type slis_t_listheader.
data: ls_line type slis_listheader.
data: wrk_name type string.
TYPES : BEGIN OF TY_CUST,
KUNNR TYPE KUNNR,
NAME1 TYPE NAME1_GP
END OF TY_CUST.
DATA : LIT_CUST TYPE TABLE OF TY_CUST,
LWA_CUST TYPE TY_CUST.
SELECT KUNNR NAME1 FROM KNA1 INTO TABLE LIT_CUST WHERE KUNNR IN S_KUNNR. “s_KUNNR IS SELECTION SCREEN SELECT OPTION FIELD.
IF LIT_CUST[] IS NOT INITIAL.
LOOP AT LIT_CUST INTO LW_CUST.
CONCATENATE LW_CUST-KUNNR ‘:’ LW_CUST-NAME1 INTO wrk_name .
ls_line-typ = 'A'.
ls_line-info = wrk_name.
append ls_line to i_top_of_page.
clear ls_line, WRK_NAME.
ENDLOOP.
ENDIF.
endform. " SUB_COMMENT_BUILD
*&---------------------------------------------------------------------*
*& Form top_of_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form top_of_page.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = t_list_top_of_page.
endform. "top_of_page
*&---------------------------------------------------------------------*
*& Form ALV_DISPLAY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form alv_display .
gs_layout-zebra = 'X'.
gs_layout-detail_popup = 'X'.
gs_layout-detail_initial_lines = 'X'.
gs_layout-colwidth_optimize = 'X'.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
it_fieldcat = it_fieldcat
it_events = it_events[]
i_callback_program = sy-repid
is_layout = gs_layout
it_sort = gt_sort[]
i_save = g_save
is_variant = gx_variant
*i_callback_pf_status_set = ' '
* i_callback_user_command = ' '
* TABLES
* T_OUTTAB = IT_FINAL[].
.
endform. " ALV_DISPLAY
Hope this will work.
Regards,
Sagar