SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software SAP ABAP Programming and HANA Database Tutorials
Development resources, articles, tutorials, code samples and tools and downloads for ASP.Net, SQL Server, R Script, Windows, Windows Phone, AWS, SAP HANA and ABAP, like SAP UI5, Screen Personas, etc.

Install SAP Free

SAP ABAP Tutorial - ALV Grid Example with cl_gui_alv_grid and Screen Painter

This ABAP ALV tutorial includes an example ABAP program which lists VBAK and VBAP sales order items between given two VBELN numbers on an SAP screen on ALV grid using cl_gui_alv_grid class.

Tutorial includes a screenshot from the SAP program showing the output of the example ABAP program that filters data from VBAK and VBAP transparent tables and displays data on an ABAP ALV grid control cl_gui_alv_grid placed in a custom container on a dynpro screen.

SAP ABAP alv grid example with vbeln range
Display data using cl_gui_alv_grid in ABAP programming

Let's begin with the source code of the example ABAP report codes.
Following ABAP codes include required data definitions, custom container and ALV grid object definitions as well as a call to SAP Dynpro screen to display ALV table with populated data.


 gr_vbeln TYPE RANGE OF vbak-vbeln,
 grs_vbeln LIKE LINE OF gr_vbeln.

 gs_fieldcatalog TYPE lvc_s_fcat OCCURS 0,
 gv_fcat LIKE LINE OF gs_fieldcatalog,
 gs_layout TYPE lvc_s_layo.

 BEGIN OF gty_item,
  mandt LIKE vbak-mandt,
  vbeln LIKE vbak-vbeln,
  erdat LIKE vbak-erdat,
  kunnr LIKE vbak-kunnr,
  posnr LIKE vbap-posnr,
  matnr LIKE vbap-matnr,
  arktx LIKE vbap-arktx,
  kwmeng LIKE vbap-kwmeng,
  desc_text LIKE zvbap-desc_text,
 END OF gty_item,
 BEGIN OF gty_vbak,
  mandt LIKE vbak-mandt,
  vbeln LIKE vbak-vbeln,
  erdat LIKE vbak-erdat,
  kunnr LIKE vbak-kunnr,
 END OF gty_vbak,
 BEGIN OF gty_vbap,
  vbeln LIKE vbap-vbeln,
  posnr LIKE vbap-posnr,
  matnr LIKE vbap-matnr,
  arktx LIKE vbap-arktx,
  kwmeng LIKE vbap-kwmeng,
 END OF gty_vbap.

 gs_item TYPE gty_item,
 gt_item TYPE TABLE OF gty_item.

 gs_vbak TYPE gty_vbak,
 gt_vbak TYPE TABLE OF gty_vbak,
 gs_vbap TYPE gty_vbap,
 gt_vbap TYPE TABLE OF gty_vbap.

 g_Container TYPE scrfname VALUE 'CC_CONTAINER_GRID',

 OK_CODE LIKE sy-ucomm,
 SAVE_OK LIKE sy-ucomm.



Let's continue reviewing the sample ABAP program code with the screen 100 dynpro object.
Double click on 100 at ABAP statement "call screen 100" to create the dynpro screen.
In the below screenshot you can see the layout view of the screen on the SAP Screen Painter editor.
This Screen Painter example shows a Custom Control custom control cl_gui_custom_container object and the ALV grid object cl_gui_alv_grid placed in that container.

If you double click on the custom control to display the element attributes, you can name the control as "CC_CONTAINER_GRID"

SAP ABAP screen painter screen for ABAP alv grid sample

Please pay attention to the FctCode (function code) of the Button object BTNLIST. The FctCode is set as "BTNLIST" and will be controlled in the PAI (Process After Input) module with the ABAP CASE statement controlling the value of OK_CODE.

I named the two text box objects which I'll use to filter sales order documents according to document number VBELN fields as follows:
The first text element name: GRS_VBELN-LOW
Second text element name as: GRS_VBELN-HIGH

SAP ABAP screen painter text element for range filter

One last step for the screen 100 configuration is assigning OK_CODE variable by adding it to the general attributes in Element list of the screen.

ABAP program screen element for OK code

Add below Process Before Output (PBO) module code or ABAP code block of the Screen 100 after all above screen element attributes are set.



 IF g_Custom_Container IS INITIAL.

  " Create CONTAINER object with reference to container name in the screen
  CREATE OBJECT g_Custom_Container EXPORTING CONTAINER_NAME = g_Container.
  " Create GRID object with reference to parent name
  CREATE OBJECT g_Grid EXPORTING I_PARENT = g_Custom_Container.

  PERFORM u_prepare_fieldcatalog.
  gs_layout-ZEBRA = 'X'.
  "gs_layout-edit = 'X'. " Makes all Grid editable

    is_layout = gs_layout
    it_fieldcatalog = gs_fieldcatalog
    IT_OUTTAB = gt_item. " Data



Double click on TITLE to create a new title bar.
Also double click on MAINSTATUS to create a PF-STATUS object.
It is enough for this ABAP tutorial to navigate on the ABAP program if you define below standard toolbar commands.

ABAP standard toolbar for dynpro screen

And you can use below Process After Input (PAI) module code or ABAP code block of the Screen 100.



   PERFORM u_filter_vbak.


In U_FILTER_VBAK, the required ABAP code to select data from related data dictionary objects, tables according to the given criteria takes place.
Within this ABAP code block, the necessary internal tables are populated with data from SAP database.


 REFRESH: gt_vbak, gt_vbap, gt_item.

 " Define Range Criteria
 grs_vbeln-SIGN = 'I'.
 grs_vbeln-OPTION = 'BT'.
 APPEND grs_vbeln to gr_vbeln.

 SELECT mandt vbeln erdat kunnr
  FROM vbak INTO TABLE gt_vbak
  WHERE vbeln IN gr_vbeln.

 SELECT vbeln posnr matnr arktx kwmeng
  FROM vbap INTO TABLE gt_vbap
  WHERE vbeln EQ gt_vbak-vbeln.

 IF gt_vbak[] IS NOT INITIAL.

  LOOP AT gt_vbap INTO gs_vbap.

   READ TABLE gt_vbak INTO gs_vbak WITH KEY vbeln = gs_vbap-vbeln.

   gs_item-mandt = gs_vbak-mandt.
   gs_item-vbeln = gs_vbak-vbeln.
   gs_item-erdat = gs_vbak-erdat.
   gs_item-kunnr = gs_vbak-kunnr.
   gs_item-posnr = gs_vbap-posnr.
   gs_item-matnr = gs_vbap-matnr.
   gs_item-arktx = gs_vbap-arktx.
   gs_item-kwmeng = gs_vbap-kwmeng.

   APPEND gs_item TO gt_item.

   CLEAR gs_item.
   CLEAR gs_vbak.
   CLEAR gs_vbap.



And below ABAP developers can see how I build the field catalog required for displaying data on ALV grid. The field catalog layout is used as a parameter for the ALV grid object's SET_TABLE_FOR_FIRST_DISPLAY method and forms the columns and display structure of the internal data.


 CLEAR gv_fcat.
 gv_fcat-fieldname = 'MANDT'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 0.
 gv_fcat-coltext = 'MANDT'.
 gv_fcat-no_out = 'X'. " Do not Display Column
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'VBELN'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 1.
 gv_fcat-coltext = 'VBELN'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'ERDAT'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 2.
 gv_fcat-coltext = 'ERDAT'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'KUNNR'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 3.
 gv_fcat-coltext = 'KUNNR'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'POSNR'.  gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 4.
 gv_fcat-coltext = 'POSNR'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'MATNR'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 5.
 gv_fcat-coltext = 'MATNR'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'ARKTX'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 6.
 gv_fcat-coltext = 'ARKTX'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'KWMENG'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 7.
 gv_fcat-coltext = 'KWMENG'.
 gv_fcat-EDIT = 'X'. " Makes field editable in Grid
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.

 CLEAR gv_fcat.
 gv_fcat-fieldname = 'DESC_TEXT'.
 gv_fcat-tabname = 'VBAP'.
 gv_fcat-col_pos = 7.
 gv_fcat-coltext = 'DESC_TEXT'.
 gv_fcat-no_out = 'X'.
 INSERT gv_fcat INTO TABLE gs_fieldcatalog.


The field catalog prepares ALV grid columns for display and sets properties of table columns.
For example, there are 7 columns displayed on the first screenshot of this ABAP tutorial. But we have 9 fields in our field catalog.

ABAP programmers can notice field catalog property "NO_OUT" is used to hide a field on the ALV grid. "no_out" enables programmers to set a column hidden on the ALV grid display but the values are still accessible when a click or double click event is triggered. So if ABAP developer needs to hide a column on ALV grid, field catalog structure no_out field can be set to "X" to hide as seen in above sample code.

I hope I could help ABAP developers with a start-to-end sample code which they can use to display data on an ALV grid table on their SAP Dynpro screens.

SAP Resources

SAP Tutorial

SAP Forums

SAP Tools

SAP Transaction Codes Table

Related Articles and Tutorials(http://www.kodyaz.com/articles/sap-abap-tutorial-alv-grid-cl_gui_alv_grid-screen-painter.aspx)
Check Existence using ABAP SELECT
Create Business Object in SAP
Delete All Data from SAP Database Table
How to Upload Data to SAP from Excel File using alsm_excel_to_internal_table Function Module
ABAP - Create Hierarchy Tree List using rs_tree_construct, rs_tree_list_display and snodetext
ABAP Tutorial - ABAP String Split Example Code
Upload Data from Excel File in ABAP using TEXT_CONVERT_XLS_TO_SAP
Create CL_GUI_ALV_GRID ALV Grid Column Header using ABAP Data Element
How to Create SAPScript Text using SO10 Transaction Code
Multi Color ALV Grid Color Alternate using ALV Layout info_fname Property
ALV Grid Color - Table Row Background Color in ALV List
SAP ABAP Tutorial - ALV Grid Example with cl_gui_alv_grid and Screen Painter
SAP Smartforms - How to Call Smartform within ABAP Program
ABAP Tutorial - SAP Split and ABAP Split String Function
SAP Tutorial - Convert Spool Request to PDF File using RSTXPDFT4 ABAP Report
Set Default Single Values List for SAP Selection Screen Parameter
ABAP Debugging using Log Point and Break Point
ABAP RegEx for ABAP Regular Expression to fetch Date from String
ABAP Runtime Error ST22 Transaction using Short Dump Analysis
ABAP Shared Memory Objects Tutorial with Sample ABAP Code
ABAP Try Catch for exception is neither caught Code Inspector Error
Add Button to ALV Table Header on SAP Web Dynpro
Add Year, Month or Day to Date Variable in ABAP using RP_CALC_DATE_IN_INTERVAL
Read Address Data using FM ADDR_GET instead of Querying ADRC Table
Traffic Lights in SAP Web Dynpro ALV Table
ABAP Authority-Check Object For User Other Than Current
SAP Code Inspector for ABAP Break-Point Statement
Call Function Module using RFC Destination in ABAP
Component Usage ALV Does Not Have an Active Component
How to Spell Numbers using ABAP Spell_Amount Function Module
Convert the table using the Database Utility (SE14)
Convert Sales Unit into Text using Function Module in Target Language
Create SAP Dynamic Variant using Date Calculation
Create SAP Transaction to Maintain ABAP Table using SM30
Database Hint Pseudo-Comment for SAP Code Inspector
Delete Entries in NAST Table using RSCLNAST ABAP Program
Dynamic Programming using Field Symbols in ABAP Reports
Prevent DYNPRO_SEND_IN_BACKGROUND Error in Background Process
Execute ABAP Program Automatically After Transport Request Import
How to Execute SAP Program in Background Task
How to Find Internal Table Row Count in ABAP
Export SAP Data to Fixed Length Text File
Get Fullname of SAP user using Function Module in ABAP Program
How to Debug Popup Screen in ABAP
How to Find User Exits in ABAP Codes of SAP Transaction
Read Integer and Decimals of a Numeric Value in ABAP Programming
ABAP LOOP Statement with At New and At End
Move Minus Sign from Right to Left of Negative Number in SAP Smartform
Primary key change not permitted for value table
Read Pricing Condition Text using ABAP in SAP Sales Document Output
SAP Web Dynpro ALV Tutorial
Read Text Symbols in Specific Language using ABAP RS_TEXTPOOL_READ
Remove ALV Buttons on SAP Web Dynpro Component
Get SAP Document Flow using SD_DOCUMENT_FLOW_GET ABAP Function Module
Export Table Contents into Text File and Download File
Search and Find SAP Transaction Code using SE93 or SEARCH_SAP_MENU
SAP Code Inspector Error: Select For Cluster Table Without Order By
ABAP Select Statement using Space instead of Null or Initial
SAP Code Inspector: Select-Statement can be Transformed
ABAP Tutorial for SET and GET PARAMETER ID Command
ABAP CONVT_NO_NUMBER Error on SP01 due SP01_WARN Parameter
How to find string length using STRLEN ABAP string function
ABAP Submit Report to Run Another ABAP Program within Code
Transport Variants of an ABAP Program into Workbench Request
Trigger ALV Events in Web Dynpro Component
TVAP Item Category and SAP Table TVAPT for Text Translations
Code Inspector: Unsecure use of FOR ALL ENTRIES
Update Multiple Variables in ABAP Select Single Statement
Function module is not released for the Internet
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement

Copyright © 2004 - 2019 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems