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 chang the color of the font in alv

Former Member
0 Kudos

hi,

how to chang the color of the font in alv cells?

can anyone help me ?

1 ACCEPTED SOLUTION

former_member188685
Active Contributor
0 Kudos
REPORT ZTESTALV.

TYPE-POOLS: SLIS.
INCLUDE <ICON>.
*- Fieldcatalog
DATA: IT_FIELDCAT  TYPE LVC_T_FCAT,
      IT_FIELDCAT1  TYPE SLIS_T_FIELDCAT_ALV..
*- For Events
DATA:IT_EVENTS TYPE SLIS_T_EVENT.

DATA:  X_FIELDCAT  TYPE LVC_S_FCAT,
        X_FIELDCAT1  TYPE SLIS_FIELDCAT_ALV.
DATA:X_LAYOUT TYPE LVC_S_LAYO.
TABLES: LIPS.
DATA: BEGIN OF IT_VBAP OCCURS 0,
      VBELN LIKE VBAP-VBELN,
      POSNR LIKE VBAP-POSNR,
      CELLCOLOR TYPE LVC_T_SCOL,
     END OF IT_VBAP.

SELECT VBELN
       POSNR
       UP TO 10 ROWS
      INTO CORRESPONDING FIELDS OF TABLE IT_VBAP
      FROM VBAP.


DATA:L_POS TYPE I VALUE 1.
CLEAR: L_POS.
L_POS = L_POS + 1.
X_FIELDCAT-SELTEXT = 'VBELN'.
X_FIELDCAT-FIELDNAME = 'VBELN'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '10'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.

X_FIELDCAT-SELTEXT = 'POSNR'.
X_FIELDCAT-FIELDNAME = 'POSNR'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '5'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.
X_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.
DATA: LS_CELLCOLOR TYPE LVC_S_SCOL.
DATA: L_INDEX TYPE SY-TABIX.
LOOP AT IT_VBAP.
  L_INDEX = SY-TABIX.
  LS_CELLCOLOR-FNAME = 'VBELN'.
  LS_CELLCOLOR-COLOR-COL = '6'.
  LS_CELLCOLOR-COLOR-INT = '1'.
  APPEND LS_CELLCOLOR TO IT_VBAP-CELLCOLOR.
  MODIFY IT_VBAP INDEX L_INDEX TRANSPORTING CELLCOLOR.

ENDLOOP.


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    I_CALLBACK_PROGRAM       = SY-REPID
    IS_LAYOUT_LVC            = X_LAYOUT
    IT_FIELDCAT_LVC          = IT_FIELDCAT
  TABLES
    T_OUTTAB                 = IT_VBAP[]
  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.

the above code changes the color of cell.

regards

vijay

14 REPLIES 14

former_member188685
Active Contributor
0 Kudos
REPORT ZTESTALV.

TYPE-POOLS: SLIS.
INCLUDE <ICON>.
*- Fieldcatalog
DATA: IT_FIELDCAT  TYPE LVC_T_FCAT,
      IT_FIELDCAT1  TYPE SLIS_T_FIELDCAT_ALV..
*- For Events
DATA:IT_EVENTS TYPE SLIS_T_EVENT.

DATA:  X_FIELDCAT  TYPE LVC_S_FCAT,
        X_FIELDCAT1  TYPE SLIS_FIELDCAT_ALV.
DATA:X_LAYOUT TYPE LVC_S_LAYO.
TABLES: LIPS.
DATA: BEGIN OF IT_VBAP OCCURS 0,
      VBELN LIKE VBAP-VBELN,
      POSNR LIKE VBAP-POSNR,
      CELLCOLOR TYPE LVC_T_SCOL,
     END OF IT_VBAP.

SELECT VBELN
       POSNR
       UP TO 10 ROWS
      INTO CORRESPONDING FIELDS OF TABLE IT_VBAP
      FROM VBAP.


DATA:L_POS TYPE I VALUE 1.
CLEAR: L_POS.
L_POS = L_POS + 1.
X_FIELDCAT-SELTEXT = 'VBELN'.
X_FIELDCAT-FIELDNAME = 'VBELN'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '10'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.

X_FIELDCAT-SELTEXT = 'POSNR'.
X_FIELDCAT-FIELDNAME = 'POSNR'.
X_FIELDCAT-TABNAME = 'ITAB'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '5'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.
X_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.
DATA: LS_CELLCOLOR TYPE LVC_S_SCOL.
DATA: L_INDEX TYPE SY-TABIX.
LOOP AT IT_VBAP.
  L_INDEX = SY-TABIX.
  LS_CELLCOLOR-FNAME = 'VBELN'.
  LS_CELLCOLOR-COLOR-COL = '6'.
  LS_CELLCOLOR-COLOR-INT = '1'.
  APPEND LS_CELLCOLOR TO IT_VBAP-CELLCOLOR.
  MODIFY IT_VBAP INDEX L_INDEX TRANSPORTING CELLCOLOR.

ENDLOOP.


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    I_CALLBACK_PROGRAM       = SY-REPID
    IS_LAYOUT_LVC            = X_LAYOUT
    IT_FIELDCAT_LVC          = IT_FIELDCAT
  TABLES
    T_OUTTAB                 = IT_VBAP[]
  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.

the above code changes the color of cell.

regards

vijay

0 Kudos

HI.vijay

your code chang the color of the cell

but i want to chang the color of the font

not the backgroud.

ths!

0 Kudos

see emphasize option in fieldcat

0 Kudos

hi,

i worked on it, but it is only possible with list_display FM.

REPORT  ZTESTALV.
TYPE-POOLS: SLIS.
INCLUDE <ICON>.
*- Fieldcatalog
DATA: IT_FIELDCAT  TYPE SLIS_T_FIELDCAT_ALV,
      IT_FIELDCAT1  TYPE SLIS_T_FIELDCAT_ALV..

DATA:  X_FIELDCAT  TYPE SLIS_FIELDCAT_ALV,
        X_FIELDCAT1  TYPE SLIS_FIELDCAT_ALV.

DATA: IT_EVENTS TYPE SLIS_T_EVENT,
      X_EVENTS TYPE SLIS_ALV_EVENT.

X_EVENTS-NAME = 'END_OF_LIST'.
X_EVENTS-FORM = 'LIST_MODIFY_OUPUT'.
APPEND X_EVENTS TO IT_EVENTS.


DATA: BEGIN OF IT_VBAP OCCURS 0,
      VBELN LIKE VBAP-VBELN,
      POSNR LIKE VBAP-POSNR,
      FLAG(1),
     END OF IT_VBAP.

SELECT VBELN
       POSNR
       UP TO 10 ROWS
      INTO TABLE IT_VBAP
      FROM VBAP.
LOOP AT IT_VBAP.

  IF SY-TABIX = 1 OR SY-TABIX = 5.
    IT_VBAP-FLAG = 'X'.
    MODIFY IT_VBAP INDEX SY-TABIX.
  ENDIF.

ENDLOOP.

DATA:L_POS TYPE I VALUE 1.
CLEAR: L_POS.
L_POS = L_POS + 1.
X_FIELDCAT-SELTEXT_M = 'VBELN'.
X_FIELDCAT-FIELDNAME = 'VBELN'.
X_FIELDCAT-TABNAME = 'IT_VBAP'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '10'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.

X_FIELDCAT-SELTEXT_M = 'POSNR'.
X_FIELDCAT-FIELDNAME = 'POSNR'.
X_FIELDCAT-TABNAME = 'IT_VBAP'.
X_FIELDCAT-COL_POS    = L_POS.
X_FIELDCAT-OUTPUTLEN = '5'.
APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.
L_POS = L_POS + 1.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
  EXPORTING
    I_CALLBACK_PROGRAM = SY-REPID
    IT_FIELDCAT        = IT_FIELDCAT
    IT_EVENTS          = IT_EVENTS
  TABLES
    T_OUTTAB           = IT_VBAP
  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  LIST_MODIFY_OUPUT
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM LIST_MODIFY_OUPUT.
  DATA: L_VBELN LIKE VBAK-VBELN,
        L_POSNR LIKE VBAP-POSNR,
        l_index type sy-index.
        clear it_vbap.
  DO 20 TIMES.
    CLEAR: L_VBELN, L_POSNR.
    READ LINE SY-INDEX INDEX SY-LSIND
         FIELD VALUE IT_VBAP-VBELN INTO L_VBELN
                     IT_VBAP-POSNR INTO L_POSNR .
"3lines are reserved for alv headings , so i am reading it form 4th line
"so 4th line is equal to 1st line of itab
    IF SY-SUBRC = 0 AND SY-INDEX GE 4.
       l_index = sy-index - 3.
      READ TABLE IT_VBAP INDEX l_index.
      IF SY-SUBRC = 0 AND IT_VBAP-FLAG = 'X'.
*-Modifying current list
        MODIFY LINE SY-INDEX INDEX SY-LSIND
                   FIELD FORMAT IT_VBAP-POSNR COLOR 6 INVERSE.
        MODIFY LINE SY-INDEX INDEX SY-LSIND
                   FIELD FORMAT IT_VBAP-VBELN COLOR 6 INVERSE.
      ENDIF.
    ENDIF.
  ENDDO.

ENDFORM.                    "LIST_MODIFY_OUPUT

reagrds

vijay

0 Kudos

HI,Vijay,

ths ! your code is very well.

but if i display the alv grid in a container in a dialog program by oo, how do it ?

ths!

0 Kudos

Hi Xiaoming Cai,

Yep, there might be some way to do that..

let me do some R&D.

run this program <b>BCALV_GRID_EDIT</b>

in output change one Booking Total to char instead of num. then it will through error, and it will make the font in red color, try to find where the color is assigning. i am also looking.

Regards

vijay

Message was edited by: Vijay Babu Dudla

0 Kudos

HI,CVIJAY.

I Have solved this problem .

ths!

ls_fcat-emphasize = 'C601' .

you can try it .

0 Kudos

Great finally your problem solved, Thanks for sharing the info.

regards

vijay

0 Kudos

this code can chang the color of a column,but can't only chang the color of one cell.

0 Kudos

Hi Xiaoming Cai,

here is the code that can make the color of font in a cell conditionally, based on some condition you can change the color of a font in a cell..check this code..this is OO alv, since you asked it for OO i did it in OO .

REPORT  ZTEST1234    MESSAGE-ID ZZ                           .
DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA: L_VALID TYPE C,
      V_FLAG,
      V_DATA_CHANGE,
      V_ROW TYPE LVC_S_ROW,
      V_COLUMN TYPE LVC_S_COL,
      V_ROW_NUM TYPE LVC_S_ROID.

*---------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION .
  PUBLIC SECTION .
    METHODS:
**Hot spot Handler
    HANDLE_HOTSPOT_CLICK FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
                      IMPORTING E_ROW_ID E_COLUMN_ID ES_ROW_NO,
**Double Click Handler
    HANDLE_DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
                                     IMPORTING E_ROW E_COLUMN ES_ROW_NO.


ENDCLASS.                    "lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
*Handle Hotspot Click
  METHOD HANDLE_HOTSPOT_CLICK .
    CLEAR: V_ROW,V_COLUMN,V_ROW_NUM.
    V_ROW  = E_ROW_ID.
    V_COLUMN = E_COLUMN_ID.
    V_ROW_NUM = ES_ROW_NO.
    MESSAGE I000 WITH V_ROW 'clicked'.
  ENDMETHOD.                    "lcl_event_handler

*Handle Double Click
  METHOD  HANDLE_DOUBLE_CLICK.

  ENDMETHOD.                    "handle_double_click
ENDCLASS.                    "LCL_EVENT_HANDLER IMPLEMENTATION

*&---------------------------------------------------------------------*
*&             Global Definitions
*&---------------------------------------------------------------------*
DATA:      G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,"Container1
            G_HANDLER TYPE REF TO LCL_EVENT_HANDLER. "handler

DATA: OK_CODE LIKE SY-UCOMM,
      SAVE_OK LIKE SY-UCOMM,
      G_CONTAINER1 TYPE SCRFNAME VALUE 'TEST',
      GS_LAYOUT TYPE LVC_S_LAYO.


*- Fieldcatalog for First and second Report
DATA: IT_FIELDCAT  TYPE  LVC_T_FCAT,
      X_FIELDCAT TYPE LVC_S_FCAT,
      LS_VARI  TYPE DISVARIANT.

*---------------------------------------------------------------------
*                START-OF_SELECTION
*---------------------------------------------------------------------
START-OF-SELECTION.
  DATA:BEGIN OF  ITAB OCCURS 0,
       VBELN LIKE LIKP-VBELN,
       POSNR LIKE LIPS-POSNR,
       CELLCOLOR TYPE LVC_T_SCOL, "required for color
       END OF ITAB.
  SELECT VBELN
         POSNR
         FROM LIPS
         UP TO 20 ROWS
         INTO CORRESPONDING FIELDS OF TABLE ITAB.

END-OF-SELECTION.
  IF NOT ITAB[] IS INITIAL.
    CALL SCREEN 100.
  ELSE.
    MESSAGE I002 WITH 'NO DATA FOR THE SELECTION'(004).
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM CREATE_AND_INIT_ALV .
  DATA: LT_EXCLUDE TYPE UI_FUNCTIONS.

  CREATE OBJECT G_CUSTOM_CONTAINER
         EXPORTING CONTAINER_NAME = G_CONTAINER1.
  CREATE OBJECT G_GRID
         EXPORTING I_PARENT = G_CUSTOM_CONTAINER.
* Set a titlebar for the grid control
  CLEAR GS_LAYOUT.
  GS_LAYOUT-GRID_TITLE = TEXT-003.
  GS_LAYOUT-ZEBRA = SPACE.
  GS_LAYOUT-CWIDTH_OPT = 'X'.
  GS_LAYOUT-NO_ROWMARK = 'X'.
  GS_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.
  CALL METHOD G_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER.

  CREATE OBJECT G_HANDLER.
  SET HANDLER G_HANDLER->HANDLE_DOUBLE_CLICK FOR G_GRID.
  SET HANDLER G_HANDLER->HANDLE_HOTSPOT_CLICK FOR G_GRID.

  DATA: LS_CELLCOLOR TYPE LVC_S_SCOL. "required for color
  DATA: L_INDEX TYPE SY-TABIX.
  "Here i am changing the color of line 1,5,10...
  "so you can change the color of font conditionally
  LOOP AT ITAB.
    L_INDEX = SY-TABIX.
    IF L_INDEX = 1 OR L_INDEX = 5 OR L_INDEX = 10.
      LS_CELLCOLOR-FNAME = 'VBELN'.
      LS_CELLCOLOR-COLOR-COL = '6'.
      LS_CELLCOLOR-COLOR-INT = '0'.
      LS_CELLCOLOR-COLOR-INV = '1'.
      APPEND LS_CELLCOLOR TO ITAB-CELLCOLOR.
      MODIFY ITAB INDEX L_INDEX TRANSPORTING CELLCOLOR.
      LS_CELLCOLOR-FNAME = 'POSNR'.
      LS_CELLCOLOR-COLOR-COL = '6'.
      LS_CELLCOLOR-COLOR-INT = '0'.
      LS_CELLCOLOR-COLOR-INV = '1'.
      APPEND LS_CELLCOLOR TO ITAB-CELLCOLOR.
      MODIFY ITAB INDEX L_INDEX TRANSPORTING CELLCOLOR.
    ENDIF.
  ENDLOOP.

* setting focus for created grid control
  CALL METHOD CL_GUI_CONTROL=>SET_FOCUS
    EXPORTING
      CONTROL = G_GRID.
* Build fieldcat and set editable for date and reason code
* edit enabled. Assign a handle for the dropdown listbox.
  PERFORM BUILD_FIELDCAT.

* Optionally restrict generic functions to 'change only'.
*   (The user shall not be able to add new lines).
  PERFORM EXCLUDE_TB_FUNCTIONS CHANGING LT_EXCLUDE.
**Vaiant to save the layout
  LS_VARI-REPORT      = SY-REPID.
  LS_VARI-HANDLE      = SPACE.
  LS_VARI-LOG_GROUP   = SPACE.
  LS_VARI-USERNAME    = SPACE.
  LS_VARI-VARIANT     = SPACE.
  LS_VARI-TEXT        = SPACE.
  LS_VARI-DEPENDVARS  = SPACE.

**Calling the Method for ALV output
  CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY
    EXPORTING
      IT_TOOLBAR_EXCLUDING = LT_EXCLUDE
      IS_VARIANT           = LS_VARI
      IS_LAYOUT            = GS_LAYOUT
      I_SAVE               = 'A'
    CHANGING
      IT_FIELDCATALOG      = IT_FIELDCAT
      IT_OUTTAB            = ITAB[].

* Set editable cells to ready for input initially
  CALL METHOD G_GRID->SET_READY_FOR_INPUT
    EXPORTING
      I_READY_FOR_INPUT = 1.

ENDFORM.                               "CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*&      Form  EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PT_EXCLUDE text
*----------------------------------------------------------------------*
FORM EXCLUDE_TB_FUNCTIONS CHANGING PT_EXCLUDE TYPE UI_FUNCTIONS.
* Only allow to change data not to create new entries (exclude
* generic functions).
  DATA LS_EXCLUDE TYPE UI_FUNC.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_MOVE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_CUT.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_PASTE.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_PASTE_NEW_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_UNDO.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
ENDFORM.                               " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*&      Form  build_fieldcat
*&---------------------------------------------------------------------*
*       Fieldcatalog
*----------------------------------------------------------------------*
FORM BUILD_FIELDCAT .
  DATA: L_POS TYPE I.
  L_POS = L_POS + 1.


  X_FIELDCAT-SCRTEXT_M = 'Delivery'(024).
  X_FIELDCAT-FIELDNAME = 'VBELN'.
  X_FIELDCAT-TABNAME = 'IT_FINAL'.
  X_FIELDCAT-COL_POS    = L_POS.
  X_FIELDCAT-NO_ZERO    = 'X'.
  X_FIELDCAT-OUTPUTLEN = '10'.
  X_FIELDCAT-HOTSPOT = 'X'.

  APPEND X_FIELDCAT TO IT_FIELDCAT.
  CLEAR X_FIELDCAT.
  L_POS = L_POS + 1.

  X_FIELDCAT-SCRTEXT_M = 'Item'(025).
  X_FIELDCAT-FIELDNAME = 'POSNR'.
  X_FIELDCAT-TABNAME = 'IT_FINAL'.
  X_FIELDCAT-COL_POS    = L_POS.
  X_FIELDCAT-OUTPUTLEN = '5'.
  APPEND X_FIELDCAT TO IT_FIELDCAT.
  CLEAR X_FIELDCAT.
  L_POS = L_POS + 1.

ENDFORM.                    " build_fieldcat
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'MAIN100'.
  SET TITLEBAR 'MAIN100'.
  IF G_CUSTOM_CONTAINER IS INITIAL.
**Initializing the grid and calling the fm to Display the O/P
    PERFORM CREATE_AND_INIT_ALV.
  ENDIF.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CASE SY-UCOMM.
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

regards

vijay

0 Kudos

HI,vijay

THS!VERY MUCH!

0 Kudos

hi all

with lvc_s_scol structure I could change the cell background colors only. Of course with INV feld we can invert the colors but how may I change the color of the fonts independently from cell background color ?

Any idea ?

thnks in advance

Former Member
0 Kudos

An example of using linecolor (ALV).

Here you have a good example of coloring rows, columns and specific cells in alvs. It comes in an example of how to use hashed tables.

For coloured rows and cols check gp_bymat

for coloured specific rows uncheck gp_bymat.

report zuseofhashedtables.
************************************************************************
** Program: ZUseOfHashedTables                                        **
************************************************************************
** Author: Horacio Zapettini                                          **
**                                                                    **
** Versions: 4.6b - 4.6c                                              **
************************************************************************
** Notes:                                                             **
**     this program shows how we can use hashed tables to improve     **
**     the responce time.                                             **
**     It shows,                                                      **
**        1. how to declare hashed tables                             **
**        2. a cache-like technique to improve access to master data  **
**        3. how to collect data using hashed tables                  **
**        4. how to avoid deletions of unwanted data                  **
************************************************************************
** Results: the test we run read about 31000 rows from mkpf, 150000   **
**          rows from mseg, 500 rows from makt and 400 from lfa1.     **
**          it filled ht_lst with 24500 rows and displayed them in    **
**          alv grid format.                                          **
**                                                                    **
**          It took about 65 secodns to perform this task (first time **
**          we run it when all the db buffers are empty.              **
**                                                                    **
**          The same program with standard tables needed 140 seconds  **
**          to run with the same recordset and with buffers filled in **
**                                                                    **
**          A simmilar test over more than a million rows
************************************************************************
** Objetive: show a list that consists of  all the material movements **
**          '101' - '901' for a certain range of dates in mkpf-budat. **
** the columns to be displayed are:                                   **
**          mkpf-budat,                                               **
**          mkpf-mblnr,                                               **
**          mseg-lifnr,                                               **
**          lfa1-name1,                                               **
**          mkpf-xblnr,                                               **
**          mseg-zeile                                                **
**          mseg-charg,                                               **
**          mseg-matnr,                                               **
**          makt-maktx,                                               **
**          mseg-erfmg,                                               **
**          mseg-erfme.                                               **
** or show a sumary list by matnr - menge                             **
**                                                                    **
** You'll have to create a pf-status called vista -                   **
** See form set_pf_status for details                                 **
************************************************************************

** tables used -
tables: mkpf,
        mseg,
        lfa1,
        makt.

** global hashed tables used
*
data: begin of wa_mkpf, "header
      mblnr like mkpf-mblnr,
      mjahr like mkpf-mjahr,
      budat like mkpf-budat,
      xblnr like mkpf-xblnr,
      end of wa_mkpf.
data: ht_mkpf like hashed table of wa_mkpf
      with unique key mblnr mjahr
      with header line.
data: st_mkpf like standard table of wa_mkpf
      with header line.

*
data: begin of wa_mseg, " line items
      mblnr like mseg-mblnr,
      mjahr like mseg-mjahr,
      zeile like mseg-zeile,
      bwart like mseg-bwart,
      charg like mseg-charg,
      matnr like mseg-matnr,
      lifnr like mseg-lifnr,
      erfmg like mseg-erfmg,
      erfme like mseg-erfme,
      end of wa_mseg.
data ht_mseg like hashed table of wa_mseg
      with unique key mblnr mjahr zeile
      with header line.
data st_mseg like standard table of wa_mseg
      with header line.


** cache structure for lfa1 records
data: begin of wa_lfa1,
      lifnr like lfa1-lifnr,
      name1 like lfa1-name1,
      end of wa_lfa1.
data ht_lfa1 like hashed table of wa_lfa1
      with unique key lifnr
      with header line.

** cache structure for material related data
data: begin of wa_material,
      matnr like makt-matnr,
      maktx like makt-maktx,
      end of wa_material.
data: ht_material like hashed table of wa_material
        with unique key matnr
        with header line.

** result table
data: begin of wa_lst, "
      budat like mkpf-budat,
      mblnr like mseg-mblnr,
      lifnr like mseg-lifnr,
      name1 like lfa1-name1,    
      xblnr like mkpf-xblnr,
      zeile like mseg-zeile,
      charg like mseg-charg,
      matnr like mseg-matnr,
      maktx like makt-maktx,
      erfmg like mseg-erfmg,
      erfme like mseg-erfme,
      mjahr like mseg-mjahr,
      end of wa_lst.

data: ht_lst like hashed table of wa_lst
        with unique key mblnr mjahr zeile
        with header line.

data: begin of wa_lst1, " sumary by material
      matnr like mseg-matnr,
      maktx like makt-maktx,
      erfmg like mseg-erfmg,
      erfme like mseg-erfme,
      color_line(4) TYPE c,           " Line color
      color_cell    TYPE lvc_t_scol,  " Cell color
      celltab type LVC_T_STYL,
      end of wa_lst1.

data: ht_lst1 like hashed table of wa_lst1
        with unique key matnr
        with header line.

** structures for alv grid display.
** itabs
type-pools: slis.

data: it_lst            like standard table of wa_lst with header line,
      it_fieldcat_lst   type slis_t_fieldcat_alv with header line,
      it_sort_lst       type slis_t_sortinfo_alv,
      it_lst1           like standard table of wa_lst1 with header line,
      it_fieldcat_lst1  type slis_t_fieldcat_alv with header line,
      it_sort_lst1      type slis_t_sortinfo_alv.
** structures
data: wa_sort         type slis_sortinfo_alv,
      ls_layout       type slis_layout_alv.
** color management.
DATA  : wa_color    TYPE lvc_s_scol.
* Internal table for color management.
DATA : it_color    TYPE TABLE          OF lvc_s_scol.

* itab for input enabling.
DATA: lt_celltab TYPE lvc_t_styl. "


** global varialbes

data: g_lines type i.

data: g_repid like sy-repid,
      ok_code       like sy-ucomm.

** selection-screen

"text: Dates:
select-options: so_budat for mkpf-budat default sy-datum.
"text: Material numbers.
select-options: so_matnr for mseg-matnr.
selection-screen uline.
selection-screen skip 1.
"Text: show summary by material.
parameters: gp_bymat as checkbox default ''.
parameters: gp_hier  as checkbox default 'X'.
start-of-selection.
  perform get_data.
  perform show_data.

end-of-selection.


*---------------------------------------------------------------------*
*       FORM get_data                                                 *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
form get_data.
        select mblnr mjahr budat xblnr
            into table ht_mkpf
           from mkpf
          where budat in so_budat. " make use of std index.
** have we retrieved data from mkpf?
  describe table ht_mkpf lines g_lines.
  if g_lines > 0.
** if true then retrieve all related records from mseg.
** Doing this way we make sure that the access is by primary key
** of mseg.
** The reason is that is faster to filter them in memory
** than to allow the db server to do it.
    select mblnr mjahr zeile bwart charg
             matnr lifnr erfmg erfme
      into table ht_mseg
      from mseg
        for all entries in ht_mkpf
     where mblnr = ht_mkpf-mblnr
       and mjahr = ht_mkpf-mjahr.
  endif.
        
** fill t_lst or t_lst1 according to user's choice.
  if gp_bymat = ' '.
    perform fill_ht_lst.
  else.
    perform fill_ht_lst1.
  endif.
endform.

form fill_ht_lst.
  refresh ht_lst.
** Example: how to discard unwanted data in an efficient way.
  loop at ht_mseg.
*   filter unwanted data
    check ht_mseg-bwart = '101' or ht_mseg-bwart = '901'.
    check ht_mseg-matnr in so_matnr.
*   read header line.
    read table ht_mkpf with table key mblnr = ht_mseg-mblnr
    mjahr = ht_mseg-mjahr.
    clear ht_lst.
*  * note : this may be faster if you specify field by field.
    move-corresponding ht_mkpf to ht_lst.
    move-corresponding ht_mseg to ht_lst.

    perform read_lfa1 using ht_mseg-lifnr changing ht_lst-name1.
    perform read_material using ht_mseg-matnr changing ht_lst-maktx.
    insert table ht_lst.
  endloop.
endform.

form fill_ht_lst1.
data: colorear.
  refresh ht_lst1.
** Example: how to discard unwanted data in an efficient way.
**          hot to simulate a collect in a faster way
  loop at ht_mseg.
*   filter unwanted data
    check ht_mseg-bwart = '101' or ht_mseg-bwart = '901'.
    check ht_mseg-matnr in so_matnr.
*  * note : this may be faster if you specify field by field.

    read table ht_lst1 with table key matnr = ht_mseg-matnr
    transporting erfmg.
    if sy-subrc <> 0. " if matnr doesn't exist in sumary table
    " insert a new record
      clear ht_lst1.
      ht_lst1-matnr = ht_mseg-matnr.
      perform read_material using ht_mseg-matnr changing ht_lst1-maktx.
      ht_lst1-erfmg = ht_mseg-erfmg.
      ht_lst1-erfme = ht_mseg-erfme.
      if colorear = ''.
        colorear = 'X'.
        refresh it_color.
        ht_lst1-color_cell[] = it_color[].
        MOVE 'C410' TO ht_lst1-color_line.
      else.
        colorear = ' '.
        refresh it_color. clear it_color.
        MOVE 'MATNR' TO wa_color-fname.
        MOVE '6'         TO wa_color-color-col.
        MOVE '1'         TO wa_color-color-int.
        MOVE '1'         TO wa_color-color-inv.
        APPEND wa_color TO it_color.
        MOVE 'MAKTX' TO wa_color-fname.
        MOVE '3'         TO wa_color-color-col.
        MOVE '1'         TO wa_color-color-int.
        MOVE '1'         TO wa_color-color-inv.
        APPEND wa_color TO it_color.

        MOVE 'ERFMG' TO wa_color-fname.
        MOVE '5'         TO wa_color-color-col.
        MOVE '1'         TO wa_color-color-int.
        MOVE '1'         TO wa_color-color-inv.

        APPEND wa_color TO it_color.
        ht_lst1-color_cell[] = it_color[].

        clear ht_lst1-color_line.

      endif.

      insert table ht_lst1.
    else." a record was found.
    " collect erfmg.  To do so, fill in the unique key and add
    " the numeric fields.
      ht_lst1-matnr = ht_mseg-matnr.
      add ht_mseg-erfmg to ht_lst1-erfmg.
      modify table ht_lst1 transporting erfmg.
    endif.
  endloop.
endform.

** implementation of cache for lfa1.
form read_lfa1 using p_lifnr changing p_name1.
        read table ht_lfa1 with table key lifnr = p_lifnr
        transporting name1.
  if sy-subrc <> 0.
    clear ht_lfa1.
    ht_lfa1-lifnr = p_lifnr.
    select single name1
       into ht_lfa1-name1
      from lfa1
    where lifnr = p_lifnr.
    if sy-subrc <> 0. ht_lfa1-name1 = 'n/a in lfa1'. endif.
    insert table ht_lfa1.
  endif.
  p_name1 = ht_lfa1-name1.
endform.

** implementation of cache for material data
form read_material using p_matnr changing p_maktx.
  read table ht_material with table key matnr = p_matnr
  transporting maktx.
  if sy-subrc <> 0.
    ht_material-matnr = p_matnr.
    select single maktx into  ht_material-maktx
      from makt
     where spras = sy-langu
       and matnr = p_matnr.
    if sy-subrc <> 0. ht_material-maktx = 'n/a in makt'. endif.
    insert table ht_material.
  endif.
  p_maktx = ht_material-maktx.
endform.
form show_data.
  if gp_hier = 'X'. "no anda.
*    perform show_hierarchicalALV.
  else.
    if gp_bymat = ' '.
      perform show_ht_lst.
    else.
      perform show_ht_lst1.
    endif.
  endif.
endform.
form show_hierarchicalALV.
st_mkpf[] = ht_mkpf[].
st_mseg[] = ht_mseg[].
call function 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
*  exporting
*   I_INTERFACE_CHECK              = ' '
*   I_CALLBACK_PROGRAM             =
*   I_CALLBACK_PF_STATUS_SET       = ' '
*   I_CALLBACK_USER_COMMAND        = ' '
*   IS_LAYOUT                      =
*   IT_FIELDCAT                    =
*   IT_EXCLUDING                   =
*   IT_SPECIAL_GROUPS              =
*   IT_SORT                        =
*   IT_FILTER                      =
*   IS_SEL_HIDE                    =
*   I_SCREEN_START_COLUMN          = 0
*   I_SCREEN_START_LINE            = 0
*   I_SCREEN_END_COLUMN            = 0
*   I_SCREEN_END_LINE              = 0
*   I_DEFAULT                      = 'X'
*   I_SAVE                         = ' '
*   IS_VARIANT                     =
*   IT_EVENTS                      =
*   IT_EVENT_EXIT                  =
*    i_tabname_header               =
*    i_tabname_item                 =
*   I_STRUCTURE_NAME_HEADER        =
*   I_STRUCTURE_NAME_ITEM          =
*    is_keyinfo                     =
*   IS_PRINT                       =
*   IS_REPREP_ID                   =
*   I_BUFFER_ACTIVE                =
*   I_BYPASSING_BUFFER             =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER        =
*   ES_EXIT_CAUSED_BY_USER         =
  tables
    t_outtab_header                = st_mkpf
    t_outtab_item                  = st_mseg
* 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.

endform.
form show_ht_lst.
  "needed because the FM can't use a hashed table.
  it_lst[] = ht_lst[].

  perform fill_layout using 'full display'
                       changing ls_layout.

  perform fill_columns_lst.
*  perform sort_lst.
  g_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program       = g_repid
            i_callback_pf_status_set = 'SET_PF_STATUS'
            is_layout                = ls_layout
            it_fieldcat              = it_fieldcat_lst[]
*            it_sort                  = it_sort_lst
       tables
            t_outtab                 = it_lst
       exceptions
            program_error            = 1
            others                   = 2.

endform.
form show_ht_lst1.
  "needed because the FM can't use a hashed table.
  it_lst1[] = ht_lst1[].

  perform fill_layout using 'Sumary by matnr'
                       changing ls_layout.

  perform fill_columns_lst1.
*  perform sort_lst.
  g_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program       = g_repid
            i_callback_pf_status_set = 'SET_PF_STATUS'
            is_layout                = ls_layout
            it_fieldcat              = it_fieldcat_lst1[]
*            it_sort                  = it_sort_lst
       tables
            t_outtab                 = it_lst1
       exceptions
            program_error            = 1
            others                   = 2.

endform.

form fill_layout using p_window_titlebar
               changing cs_layo type slis_layout_alv.
  clear cs_layo.
  cs_layo-window_titlebar        = p_window_titlebar.
  cs_layo-edit                   = 'X'.
  cs_layo-edit_mode              = space.
  MOVE 'COLOR_LINE' TO cs_layo-info_fieldname.

* Field that identify cell color in inetrnal table
  MOVE 'COLOR_CELL' TO cs_layo-coltab_fieldname.
*  move 'CELLTAB' TO cs_layo-stylefname.


endform.                    " armar_layout_stock

form set_pf_status using rt_extab type slis_t_extab.
** create a new status
** and then select extras -> adjust template -> listviewer
  set pf-status 'VISTA'.
endform.        "set_pf_status
define add_lst.
  clear it_fieldcat_lst.
  it_fieldcat_lst-fieldname     = &1.
  it_fieldcat_lst-outputlen     = &2.
  it_fieldcat_lst-ddictxt       = 'L'.
  it_fieldcat_lst-seltext_l       = &1.
  it_fieldcat_lst-seltext_m       = &1.
  it_fieldcat_lst-seltext_m       = &1.
  if &1 = 'MATNR'.
    it_fieldcat_lst-emphasize = 'C111'.
  endif.
  append it_fieldcat_lst.
end-of-definition.
define add_lst1.
  clear it_fieldcat_lst.
  it_fieldcat_lst1-fieldname     = &1.
  it_fieldcat_lst1-outputlen     = &2.
  it_fieldcat_lst1-ddictxt       = 'L'.
  it_fieldcat_lst1-seltext_l       = &1.
  it_fieldcat_lst1-seltext_m       = &1.
  it_fieldcat_lst1-seltext_m       = &1.
  append it_fieldcat_lst1.
end-of-definition.

form fill_columns_lst.
* set columns for output.

  refresh it_fieldcat_lst.
*
  add_lst 'BUDAT' 10.
  add_lst   'MBLNR' 10.
  add_lst  'LIFNR' 10.
  add_lst  'NAME1' 35.
  add_lst  'XBLNR' 15.
  add_lst    'ZEILE' 5.
  add_lst    'CHARG' 10.
  add_lst   'MATNR' 18.
  add_lst   'MAKTX' 30.
  add_lst   'ERFMG' 17.
  add_lst   'ERFME' 5.
  add_lst   'MJAHR' 4.
endform.
form fill_columns_lst1.
* set columns for output.

  refresh it_fieldcat_lst1.

  add_lst1 'MATNR' 18.
  add_lst1 'MAKTX' 30.
  add_lst1 'ERFMG' 17.
  add_lst1 'ERFME' 5..
endform.

Horacio Zapettini

--------------------------------
Program to Calculate FI Opening Balance

How to find the Opening balance for a given period in FI Module for a Particular GL A/c.


I was calculated opening balance, code is below maybe it will be helpful.

*find period.
  CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
    EXPORTING
      i_date         = s_budat-low
      i_periv        = i_tab-periv                          "'K4'
    IMPORTING
      e_buper        = v_donem
      e_gjahr        = v_gjahr
    EXCEPTIONS
      input_false    = 1
      t009_notfound  = 2
      t009b_notfound = 3
      OTHERS         = 4.
*calc opening balance hesabý
  SELECT * FROM knc1 WHERE kunnr = i_tab-kunnr
                     AND bukrs = i_tab-bukrs " s_bukrs
                     AND gjahr EQ v_gjahr.

    v_dnm = v_donem.
* opening balance first calc > old year ,
    WHILE v_dnm > 1.
      v_dnm = v_dnm - 1.
      CONCATENATE 'knc1-um' v_dnm 's' INTO v_field_name_borc.
      CONCATENATE 'knc1-um' v_dnm 'h' INTO v_field_name_alacak.
      ASSIGN (v_field_name_borc) TO <fs1> .
      ASSIGN (v_field_name_alacak) TO <fs2> .
      i_tab-dmbtr_s = i_tab-dmbtr_s + ( <fs1>  ). " borc
      i_tab-dmbtr_h = i_tab-dmbtr_h + ( <fs2>  ). " borc
    ENDWHILE.

*opening balance last calc> old
* add days which is from selected date-low month
    IF v_donem > 1.
      v_dnm = v_donem - 1.
    ELSE.
      v_dnm = v_donem.
    ENDIF.

    SELECT SINGLE * FROM t009b WHERE periv = i_tab-periv    "'K4'
                                 AND bdatj = s_budat-low+0(4)
                                 AND poper = v_dnm.

    t009b-butag = t009b-butag + 1.
    IF s_budat-low+6(2) NE t009b-butag.
      v_date_high = s_budat-low - 1.
      IF v_donem = 1.
        v_date_low = s_budat-low.
        v_date_low+4(4)  = '0101'.
      ELSE.
        CONCATENATE t009b-bdatj t009b-bumon t009b-butag INTO
        v_date_low.
      ENDIF.
      SELECT *  FROM bsad WHERE bukrs EQ i_tab-bukrs "IN s_bukrs
                            AND kunnr = i_tab-kunnr
                            AND budat BETWEEN v_date_low AND
                            v_date_high
                            AND umskz = space
                            AND blart IN s_blart.

        IF bsad-shkzg = 'S'.
          i_tab-dmbtr_s = i_tab-dmbtr_s + ( bsad-dmbtr ).
        ELSEIF bsad-shkzg = 'H'.
          i_tab-dmbtr_h = i_tab-dmbtr_h + ( bsad-dmbtr   ).
        ENDIF.
      ENDSELECT.
      SELECT *  FROM bsid WHERE bukrs EQ i_tab-bukrs "IN s_bukrs
                           AND kunnr = i_tab-kunnr
                           AND budat BETWEEN v_date_low AND
                           v_date_high
                           AND umskz = space
                           AND blart IN s_blart.
*                              AND gsber IN gsber.
        IF bsid-shkzg = 'S'.
          i_tab-dmbtr_s = i_tab-dmbtr_s + ( bsid-dmbtr   ).
        ELSEIF bsid-shkzg = 'H'.
          i_tab-dmbtr_h = i_tab-dmbtr_h + ( bsid-dmbtr   ).
        ENDIF.
      ENDSELECT.
    ENDIF.
    "opening balance ( þirket bazlý )z1 degeri
   i_tab-z1 = i_tab-z1 + ( knc1-umsav + i_tab-dmbtr_s - i_tab-dmbtr_h ).
* for israel
    i_tab-dmbtril_s = i_tab-dmbtr_s .
    i_tab-dmbtril_h = i_tab-dmbtr_h .
  ENDSELECT.