In Previous article I have provided an overview of Classical Report in SAP, in this article I will provide Code to show data of database table in ALV Grid Report. We will also see how to edit a particular field from that report in database table.
Please see the Source code below, you can use this as is with changes as required.
REPORT ZBILL_ALV_REPORT. *&---------------------------------------------------------------------* *& Report ZBILL_ALV_GRID_REPORT *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* TABLES: ZBILL. TYPE-POOLS SLIS. " ---- WORK-AREA AND INTERNAL TABLE DECLARATION ---- DATA: WA_BILL TYPE ZBILL, IT_BILL TYPE ZBILL OCCURS 0. DATA: REP_ID TYPE SY-REPID VALUE SY-REPID. DATA: *-----DECLARING THE FIELD CATALOG WORK AREA & INTERNAL TABLE-----------* WA_FCAT TYPE SLIS_FIELDCAT_ALV, IT_FCAT TYPE SLIS_T_FIELDCAT_ALV, *-----DECLARING THE WORK AREA OF ALV LAYOUT----------------------------* WA_LAYOUT TYPE SLIS_LAYOUT_ALV, LS_VARIANT type DISVARIANT, *-----DECLARING THE WORK AREA & INTERNAL TABLE FOR TOP OF PAGE---------* WA_TOP TYPE SLIS_LISTHEADER, IT_TOP TYPE SLIS_T_LISTHEADER. START-OF-SELECTION. " for selection of data from database. * SET PF-STATUS 'SAVESTS'. PERFORM GET_BILLS. END-OF-SELECTION. PERFORM ALV_LAYOUT. PERFORM ALV_FIELD_CATALOG. PERFORM ALV_GRID_DISPLAY. TOP-OF-PAGE. PERFORM TOP_OF_BILL. *&---------------------------------------------------------------------* *& Form GET_BILLS *&---------------------------------------------------------------------* * Selection of BILL table data *----------------------------------------------------------------------* FORM GET_BILLS. SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_BILL FROM ZBILL. * SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_BILL FROM ZBILL WHERE DOC_ID IN P_DOC_ID. IF SY-SUBRC = 0. SORT IT_BILL. ELSE. MESSAGE 'Bill Records doesn''t exist' TYPE 'I'. ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& Form alv_field_catalog *&---------------------------------------------------------------------* * Preparing ALV field catalog *----------------------------------------------------------------------* FORM ALV_FIELD_CATALOG. DATA LV_COL TYPE I VALUE 0. IF NOT IT_BILL IS INITIAL. LV_COL = 1 + LV_COL. "INCREMENTING POSITION OF COLUMN WA_FCAT-COL_POS = LV_COL. "COLUMN POSITION WA_FCAT-FIELDNAME = 'DOC_ID'. "TECHNICAL FIELD NAME WA_FCAT-TABNAME = 'IT_BILL'. "OUTPUT TABLE NAME WA_FCAT-SELTEXT_L = 'Document ID'. "FIELD TEXT WA_FCAT-EMPHASIZE = 'C10'. "FOR COLUMN COLOR * WA_LAYOUT-COLTAB_FIELDNAME = 'CELLCOLOR'. APPEND WA_FCAT TO IT_FCAT. "PREPARING THE FIELDCAT TABLE CLEAR WA_FCAT. LV_COL = 1 + LV_COL. WA_FCAT-COL_POS = LV_COL. "COLUMN POSITION WA_FCAT-FIELDNAME = 'BILNO'. "TECHNICAL FIELD NAME WA_FCAT-TABNAME = 'IT_BILL'. "OUTPUT TABLE NAME WA_FCAT-SELTEXT_L = 'Bill Number'. "FIELD TEXT WA_FCAT-EMPHASIZE = 'C21'. APPEND WA_FCAT TO IT_FCAT. "PREPARING THE FIELDCAT TABLE CLEAR WA_FCAT. LV_COL = 1 + LV_COL. WA_FCAT-COL_POS = LV_COL. "COLUMN POSITION WA_FCAT-FIELDNAME = 'BILDESC'. "TECHNICAL FIELD NAME WA_FCAT-TABNAME = 'IT_BILL'. "OUTPUT TABLE NAME WA_FCAT-SELTEXT_L = 'Description'. "FIELD TEXT WA_FCAT-EMPHASIZE = 'C21'. APPEND WA_FCAT TO IT_FCAT. "PREPARING THE FIELDCAT TABLE CLEAR WA_FCAT. LV_COL = 1 + LV_COL. WA_FCAT-COL_POS = LV_COL. "COLUMN POSITION WA_FCAT-FIELDNAME = 'QUANTITY'. "TECHNICAL FIELD NAME WA_FCAT-TABNAME = 'IT_BILL'. "OUTPUT TABLE NAME WA_FCAT-SELTEXT_L = 'Total Quantity'. "FIELD TEXT WA_FCAT-EMPHASIZE = 'C21'. APPEND WA_FCAT TO IT_FCAT. "PREPARING THE FIELDCAT TABLE CLEAR WA_FCAT. LV_COL = 1 + LV_COL. WA_FCAT-COL_POS = LV_COL. "COLUMN POSITION WA_FCAT-FIELDNAME = 'AMOUNT'. "TECHNICAL FIELD NAME WA_FCAT-TABNAME = 'IT_BILL'. "OUTPUT TABLE NAME WA_FCAT-SELTEXT_L = 'AMOUNT'. "FIELD TEXT WA_FCAT-EMPHASIZE = 'C30'. * WA_FCAT-EDIT = 'X'. WA_FCAT-DO_SUM = 'X'. APPEND WA_FCAT TO IT_FCAT. "PREPARING THE FIELDCAT TABLE CLEAR WA_FCAT. LV_COL = 1 + LV_COL. WA_FCAT-COL_POS = LV_COL. "COLUMN POSITION WA_FCAT-FIELDNAME = 'BILPMOD'. "TECHNICAL FIELD NAME WA_FCAT-TABNAME = 'IT_BILL'. "OUTPUT TABLE NAME WA_FCAT-SELTEXT_L = 'Payment Mode'. "FIELD TEXT * WA_FCAT-EMPHASIZE = 'C2'. WA_FCAT-EDIT = 'X'. APPEND WA_FCAT TO IT_FCAT. "PREPARING THE FIELDCAT TABLE CLEAR WA_FCAT. ENDIF. ENDFORM. " end alv_field_catalog *&---------------------------------------------------------------------* *& Form alv_layout *&---------------------------------------------------------------------* * Preparing the ALV Layout *----------------------------------------------------------------------* FORM ALV_LAYOUT. WA_LAYOUT-ZEBRA = 'X'. "CALLING THE ZEBRA LAYOUT WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'. ENDFORM. FORM TOP_OF_BILL. REFRESH IT_TOP. WA_TOP-TYP = 'H'. "HEADER TYPE WA_TOP-INFO = 'Technocrats Club'. "HEADER TEXT APPEND WA_TOP TO IT_TOP. * WA_TOP-TYP = 'S'. "HEADER TYPE * WA_TOP-KEY = 'Bill Records'. "HEADER TEXT * * APPEND WA_TOP TO IT_TOP. * CLEAR WA_TOP. WA_TOP-TYP = 'S'. "NORMAL LINE TYPE WA_TOP-KEY = 'Report:'. "NORMAL LINE TEXT WA_TOP-INFO = SY-REPID. * CONCATENATE WA_TOP-KEY SY-REPID INTO WA_TOP-INFO SEPARATED BY SPACE. "CONCATENATING THE TEXT INFO WITH PROGRAM NAME APPEND WA_TOP TO IT_TOP. CLEAR WA_TOP. WA_TOP-TYP = 'S'. WA_TOP-KEY = 'User:'. WA_TOP-INFO = SY-UNAME. * CONCATENATE WA_TOP-INFO SY-UNAME INTO WA_TOP-INFO SEPARATED BY SPACE. "CONCATENATING THE TEXT INFO WITH USER NAME APPEND WA_TOP TO IT_TOP. CLEAR WA_TOP. DATA: DATUM type c LENGTH 10. " CONVERT DATE TO EXTERNAL. WRITE SY-DATUM TO DATUM. WA_TOP-TYP = 'S'. WA_TOP-KEY = 'Date:'. WA_TOP-INFO = DATUM. * CONCATENATE WA_TOP-INFO SY-DATUM INTO WA_TOP-INFO SEPARATED BY SPACE. "CONCATENATING THE TEXT INFO WITH USER NAME APPEND WA_TOP TO IT_TOP. CLEAR WA_TOP. DATA : S_TIME TYPE C LENGTH 20. "TIME CONVERSION TO EXTERNAL WRITE SY-UZEIT TO S_TIME. WA_TOP-TYP = 'S'. WA_TOP-KEY = 'Time:'. WA_TOP-INFO = S_TIME. * CONCATENATE WA_TOP-INFO SY-UZEIT INTO WA_TOP-INFO SEPARATED BY SPACE. "CONCATENATING THE TEXT INFO WITH USER NAME APPEND WA_TOP TO IT_TOP. CLEAR WA_TOP. "-CALLING FUNCTION MODULE FOR DISPLAYING TOP OF PAGE-------------------* CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING IT_LIST_COMMENTARY = IT_TOP "PASSING THE INTERNAL TABLE I_LOGO = 'ENJOYSAP_LOGO' * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. *&---------------------------------------------------------------------* *& Form alv_grid_display *&---------------------------------------------------------------------* * Preparing the final output by using Grid Display *----------------------------------------------------------------------* FORM ALV_GRID_DISPLAY. IF NOT IT_BILL IS INITIAL AND NOT IT_FCAT IS INITIAL. * CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' * CALL FUNCTION 'CL_GUI_ALV_GRID' EXPORTING * I_INTERFACE_CHECK = ' ' * I_BYPASSING_BUFFER = ' ' * I_BUFFER_ACTIVE = ' ' I_CALLBACK_PROGRAM = SY-REPID I_CALLBACK_PF_STATUS_SET = 'PF' I_CALLBACK_USER_COMMAND = 'USER_COMMAND' I_CALLBACK_TOP_OF_PAGE = 'TOP_OF_BILL' * I_CALLBACK_HTML_TOP_OF_PAGE = ' ' * I_CALLBACK_HTML_END_OF_LIST = ' ' * I_STRUCTURE_NAME = * I_BACKGROUND_ID = ' ' I_GRID_TITLE = 'Bill Records' * I_GRID_SETTINGS = IS_LAYOUT = WA_LAYOUT IT_FIELDCAT = IT_FCAT * IT_EXCLUDING = * IT_SPECIAL_GROUPS = * IT_SORT = ITSORT * IT_EXCLUDING = * IT_SPECIAL_GROUPS = * IT_FILTER = * IS_SEL_HIDE = * I_DEFAULT = 'X' * I_SAVE = 'X' * IS_VARIANT = LS_VARIANT * IT_EVENTS = LI_EVENTS * IT_EVENT_EXIT = * IS_PRINT = * IS_REPREP_ID = * I_SCREEN_START_COLUMN = 0 * I_SCREEN_START_LINE = 0 * I_SCREEN_END_COLUMN = 0 * I_SCREEN_END_LINE = 0 * I_HTML_HEIGHT_TOP = 0 * I_HTML_HEIGHT_END = 0 * IT_ALV_GRAPHICS = * IT_HYPERLINK = * IT_ADD_FIELDCAT = * IT_EXCEPT_QINFO = * IR_SALV_FULLSCREEN_ADAPTER = TABLES T_OUTTAB = IT_BILL. IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDIF. ENDFORM. FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD. CASE R_UCOMM. WHEN 'SAVE'." INSERTING NEW FIELD VALUE IN DATABASE TABLE. LOOP AT IT_BILL INTO WA_BILL WHERE BILPMOD IS NOT INITIAL. UPDATE ZBILL SET BILPMOD = WA_BILL-BILPMOD UNAME = SY-UNAME BILDATE = SY-DATUM BILTIME = SY-UZEIT WHERE DOC_ID = WA_BILL-DOC_ID. ENDLOOP. IF SY-SUBRC EQ 0. MESSAGE 'RECORD UPDATED SUCCESSFULLY' TYPE 'I'. ENDIF. ENDFORM. FORM PF USING RT_EXTAB TYPE SLIS_T_EXTAB. SET PF-STATUS 'SAVESTS'. ""<--PASS YOUR PF-STATUS CREATED ENDFORM.
Out Put: