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: 

Editting a cell in ALV Grid Control

Former Member
0 Kudos

Hello,

Can anybody please tell me how to make a cell "EDITABLE" in alv grid control. When i use the EDIT option of field catalog, complete column gets editable. But I want only one column to get editable.

Regards

Kalyan

11 REPLIES 11

Former Member
0 Kudos

Hi kalyan,

When u want to do edit only for particular column and not for the entire grid then you need to perform fieldcat in that particular column only.

for example

form build_fieldcat changing pt_fieldcat type lvc_t_fcat.

data ls_fcat type lvc_s_fcat.

loop at pt_fieldcat into ls_fcat.

if ls_fcat-fieldname EQ 'Curr'.

ls_fcat-scrtext_s = 'CURRENCY'.

ls_fcat-scrtext_m = 'CURRENCY'.

ls_fcat-scrtext_l = 'CURRENCY'.

ls_fcat-reptext = 'CURRENCY'.

Ls_fcat-seltext = 'CURRENCY'.

<b>ls_fcat-edit = 'X'.</b>

modify pt_fieldcat from ls_fcat.

endif.

endloop.

in this way you can give editable option to that particular column only.

Regards,

venu.

0 Kudos

Hello Venu,

Thanks for you answer. But as I specified earlier, I want to make a single cell editable not the complete column editable.

Regards

Kalyan

SandeepJha
Product and Topic Expert
Product and Topic Expert
0 Kudos

This is taken from a reference doc which was here on SDN few months back.

1. In the fieldcatalogue - don't set the edit property for the field.

2. add a table to your list data table.

data begin of gt_grid occurs 0.

.

.

data cellstyles type lvc_t_styl.

data end of gt_grid.

3. Then now the logic ..

form make_field_edit using pt_grid like gt_grid[].

data: ls_grid like line of pt_grid,

ls_style type lvc_s_styl,

lt_style type lvc_t_styl.

loop at pt_grid into ls_grid.

if ls_grid-curr = xyz. 'your condition for the row

ls_style-fieldname = 'CURR'.

ls_style-style = cl_gui_alv_grid=>mc_style_enabled.

append ls_style to lt_style.

endif.

insert lines of lt_style into ls_grid-celltyles.

modify pt_grid from ls_grid.

endloop.

endform.

0 Kudos

hi kalyan,

here is the right example for your requirement.

PROGRAM BCALV_EDIT_02.

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

  • Purpose:

  • ~~~~~~~~

  • This report illustrates how to set chosen cells of an

  • ALV Grid Control editable. (See BCALV_EDIT_01 for an overview

  • of possible states).

  • Remark: You may set the states for chosen columns using field

  • EDIT of the fieldcatalog, see BCALV_EDIT_03.

*----

-


  • To check program behavior

  • ~~~~~~~~~~~~~~~~~~~~~~~~~

  • Switch to the state editable activated. You may then change the

  • price of flights where the capacity of a plane is greater or equal

  • than 300 seats.

*----

-


  • Essential steps (search for '§')

  • ~~~~~~~~~~~~~~~

  • 1.Extend your output table for a field, e.g., CELLTAB, that holds

  • information about the edit status of each cell for the

  • corresponding row (the table type is SORTED!).

  • 2.After selecting data, set edit status for each row in a loop

  • according to field SEATSMAX.

  • 2a.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_ENABLED to set a cell

  • to status "editable".

  • 2b.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_DISABLED to set a cell

  • to status "non-editable".

  • 2c.Copy your celltab to the celltab of the current row of gt_outtab.

  • 3.Provide the fieldname of the celltab field by using field

  • STYLEFNAME of the layout structure.

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

DATA: ok_code LIKE sy-ucomm,

save_ok like sy-ucomm,

g_container TYPE scrfname VALUE 'BCALV_GRID_DEMO_0100_CONT1',

grid1 TYPE REF TO cl_gui_alv_grid,

g_custom_container TYPE REF TO cl_gui_custom_container,

gs_layout TYPE lvc_s_layo,

g_max type i value 100.

*§1.Extend your output table for a field, e.g., CELLTAB, that holds

  • information about the edit status of each cell for the

  • corresponding row (the table type is SORTED!).

DATA: BEGIN OF gt_outtab occurs 0. "with header line

include structure sflight.

DATA: celltab type LVC_T_STYL.

DATA: END OF gt_outtab.

*----


*

  • MAIN *

*----


*

CALL SCREEN 100.

*----


*

  • MODULE PBO OUTPUT *

*----


*

MODULE pbo OUTPUT.

SET PF-STATUS 'MAIN100'.

SET TITLEBAR 'MAIN100'.

IF g_custom_container IS INITIAL.

CREATE OBJECT g_custom_container

EXPORTING container_name = g_container.

CREATE OBJECT grid1

EXPORTING i_parent = g_custom_container.

PERFORM select_data_and_init_style.

*§3.Provide the fieldname of the celltab field by using field

  • STYLEFNAME of the layout structure.

gs_layout-stylefname = 'CELLTAB'.

  • set substate of editable cells to deactivated

CALL METHOD grid1->set_ready_for_input

EXPORTING i_ready_for_input = 0.

CALL METHOD grid1->set_table_for_first_display

EXPORTING i_structure_name = 'SFLIGHT'

is_layout = gs_layout

CHANGING it_outtab = gt_outtab[].

ENDIF.

ENDMODULE.

*----


*

  • MODULE PAI INPUT *

*----


*

MODULE pai INPUT.

save_ok = ok_code.

clear ok_code.

CASE save_ok.

WHEN 'EXIT'.

PERFORM exit_program.

WHEN 'SWITCH'.

PERFORM switch_edit_mode.

WHEN OTHERS.

  • do nothing

ENDCASE.

ENDMODULE.

*----


*

  • FORM EXIT_PROGRAM *

*----


*

FORM exit_program.

LEAVE PROGRAM.

ENDFORM.

*&----


*

*& Form SELECT_DATA_AND_INIT_STYLE

*&----


*

  • text

*----


*

  • --> p1 text

  • <-- p2 text

*----


*

FORM select_data_and_init_style.

DATA: lt_sflight TYPE TABLE OF sflight WITH HEADER LINE,

lt_celltab TYPE lvc_t_styl,

l_index TYPE i.

SELECT * FROM sflight INTO TABLE lt_sflight UP TO g_max ROWS.

  • move corresponding fields from lt_sflight to gt_outtab

LOOP AT lt_sflight.

MOVE-CORRESPONDING lt_sflight TO gt_outtab.

APPEND gt_outtab.

ENDLOOP.

*§2.After selecting data, set edit status for each row in a loop

  • according to field SEATSMAX.

LOOP AT gt_outtab.

l_index = sy-tabix.

refresh lt_celltab.

if gt_outtab-seatsmax ge 300.

perform fill_celltab using 'RW'

changing lt_celltab.

else.

perform fill_celltab using 'RO'

changing lt_celltab.

endif.

*§2c.Copy your celltab to the celltab of the current row of gt_outtab.

INSERT LINES OF lt_celltab INTO TABLE gt_outtab-celltab.

MODIFY gt_outtab INDEX l_index.

ENDLOOP.

ENDFORM. " SELECT_DATA_AND_INIT_STYLE

*&----


*

*& Form FILL_CELLTAB

*&----


*

  • text

*----


*

  • <--P_PT_CELLTAB text

*----


*

FORM fill_celltab using value(p_mode)

CHANGING pt_celltab TYPE lvc_t_styl.

DATA: ls_celltab TYPE lvc_s_styl,

l_mode type raw4.

  • This forms sets the style of column 'PRICE' editable

  • according to 'p_mode' and the rest to read only either way.

IF p_mode EQ 'RW'.

*§2a.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_ENABLED to set a cell

  • to status "editable".

l_mode = cl_gui_alv_grid=>mc_style_enabled.

ELSE. "p_mode eq 'RO'

*§2b.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_DISABLED to set a cell

  • to status "non-editable".

l_mode = cl_gui_alv_grid=>mc_style_disabled.

ENDIF.

ls_celltab-fieldname = 'CARRID'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'CONNID'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'FLDATE'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'PRICE'.

ls_celltab-style = l_mode.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'CURRENCY'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'PLANETYPE'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'SEATSMAX'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'SEATSOCC'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ls_celltab-fieldname = 'PAYMENTSUM'.

ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.

INSERT ls_celltab INTO TABLE pt_celltab.

ENDFORM. " FILL_CELLTAB

*&----


*

*& Form SWITCH_EDIT_MODE

*&----


*

  • text

*----


*

  • --> p1 text

  • <-- p2 text

*----


*

FORM switch_edit_mode.

IF grid1->is_ready_for_input( ) eq 0.

  • set edit enabled cells ready for input

CALL METHOD grid1->set_ready_for_input

EXPORTING i_ready_for_input = 1.

ELSE.

  • lock edit enabled cells against input

CALL METHOD grid1->set_ready_for_input

EXPORTING i_ready_for_input = 0.

ENDIF.

ENDFORM. " SWITCH_EDIT_MODE

regards,

venu.

ssimsekler
Active Contributor
0 Kudos

Hi Kalyan

You can make use of the tutorial <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/a1-8-4/an%20easy%20reference%20for%20alv%20grid%20control.pdf">"An Easy eference for ALV Grid Control"</a>.

Regards

*--Serdar <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]</a>

Former Member
0 Kudos

Hi Guys,

On the example show by the others here before, it is just only apply 1 style for a cell, like below:

<b>ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.</b>

But im having a requirement to have more than 1 style need to apply to a cell. For my case, i need to apply the hotspot for the same cell in addition. where i put in the code like this:

ls_celltab-style<b>2</b> = cl_gui_alv_grid=>mc_style_hotspot.

But system will <b>NOT</b> have any effects on the value assigned in ls_celltab-style<b>2</b>. Meaning system will only recognize value from ls_celltab-style but <b>NOT</b> for ls_celltab-style2/style3/style4

Pls comment if i missed out any steps...

Thanks in advance.

Former Member
0 Kudos

Can I use this in ALV Function (REUSE_ALV_GRID_DISPLAY)??

I have a program that was done a long time ago for another person, and I don't wanna to change all the program to use control... The author put the all the collum editable (using fieldcat) and if the value can be change, he show a message. But now the user want to change this, and only enable to edit what can be edited.

thank you all....

Former Member
0 Kudos

Hi Priya,

In this am doing coloring the filed and edit the field.without header line.

TABLES:     ekko.

TYPE-POOLS: slis.                                 "ALV Declarations

*Data Declaration

*----------------

TYPES: BEGIN OF t_ekko,

   ebeln TYPE ekpo-ebeln,

   ebelp TYPE ekpo-ebelp,

   statu TYPE ekpo-statu,

   aedat TYPE ekpo-aedat,

   matnr TYPE ekpo-matnr,

   menge TYPE ekpo-menge,

   meins TYPE ekpo-meins,

   netpr TYPE ekpo-netpr,

   peinh TYPE ekpo-peinh,

   line_color(4) TYPE c,     "Used to store row color attributes

  END OF t_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,

       wa_ekko TYPE t_ekko.

*ALV data declarations

DATA: fieldcatalog TYPE LINE OF slis_t_fieldcat_alv,

       t_fieldcatalog TYPE slis_t_fieldcat_alv,

       gd_tab_group TYPE slis_t_sp_group_alv,

       gd_layout    TYPE slis_layout_alv,

       gd_repid     LIKE sy-repid.

************************************************************************

*Start-of-selection.

START-OF-SELECTION.

   PERFORM data_retrieval.

   PERFORM build_fieldcatalog ."CHANGING fieldcatalog.

   PERFORM build_layout.

   PERFORM display_alv_report.

*&---------------------------------------------------------------------*

*&      Form  BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*

*       Build Fieldcatalog for ALV Report

*----------------------------------------------------------------------*

FORM build_fieldcatalog ."CHANGING fieldcatalog TYPE slis_t_fieldcat_alv.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

    EXPORTING

      i_program_name               = sy-repid

*     I_INTERNAL_TABNAME           =

      i_structure_name             = 'ZALV_VIJAY'

*     I_CLIENT_NEVER_DISPLAY       = 'X'

*     I_INCLNAME                   =

*     I_BYPASSING_BUFFER           =

*     I_BUFFER_ACTIVE              =

     CHANGING

       ct_fieldcat                  = t_fieldcatalog

LOOP AT t_fieldcatalog INTO fieldcatalog WHERE fieldname = 'EBELN'.

   fieldcatalog-edit = 'X'.

   MODIFY t_fieldcatalog FROM fieldcatalog.

ENDLOOP.

ENDFORM.

FORM build_layout.

   gd_layout-no_input          = 'X'.

   gd_layout-colwidth_optimize = 'X'.

   gd_layout-totals_text       = 'Totals'(201).

* Set layout field for row attributes(i.e. color)

   gd_layout-info_fieldname =      'LINE_COLOR'.

ENDFORM.


FORM display_alv_report.

   gd_repid = sy-repid.

   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

        EXPORTING

             i_callback_program      = gd_repid

              is_layout               = gd_layout

             it_fieldcat             = t_fieldcatalog

             i_save                  = 'X'

            

          TABLES

             t_outtab                = it_ekko

             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 data_retrieval.

   DATA: ld_color(1) TYPE c.

   SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh

    UP TO 10 ROWS

     FROM ekpo

     INTO TABLE it_ekko.

LOOP AT it_ekko INTO wa_ekko.

ld_color = ld_color + 1.

IF ld_color = 8.

       ld_color = 1.

     ENDIF.

     CONCATENATE 'C' ld_color '10' INTO wa_ekko-line_color.

*  wa_ekko-line_color = 'C410'.

     MODIFY it_ekko FROM wa_ekko.

   ENDLOOP.

ENDFORM. 







Clemenss
Active Contributor
0 Kudos

ask google.

Former Member
0 Kudos

Hai priya,

I also faced the same  Pbm.by using the following code we can edit single row.

*&---------------------------------------------------------------------*

*&      Form  set_specific_field_attributes

*&---------------------------------------------------------------------*

*       populate FIELD_STYLE table with s pecific field attributes

*----------------------------------------------------------------------*

FORM set_specific_field_attributes.

  LOOP AT it_makt INTO wa_makt.

    IF wa_makt-indcat NE 'H'.(By using the condition iam editing single row)

      PERFORM field_attributes USING 'INDCAT' 'X'.

      PERFORM field_attributes USING 'ZMRDAT'  'X'.

      PERFORM field_attributes USING 'MINO'  'X'.

      PERFORM field_attributes USING 'ERDAT'  'X'.

      PERFORM field_attributes USING 'EMPID'  'X'.

      PERFORM field_attributes USING 'ERNAM'  'X'.

      PERFORM field_attributes USING 'MATNR'  'X'.

      PERFORM field_attributes USING 'KZECH'  'X'.

      PERFORM field_attributes USING 'RAUBE'  'X'.

      PERFORM field_attributes USING 'BEHVO'  'X'.

      PERFORM field_attributes USING 'LGPBE'  'X'.

      PERFORM field_attributes USING 'EKGRP'  'X'.

      PERFORM field_attributes USING 'DISPR'  'X'.

      PERFORM field_attributes USING 'PRCTR'  'X'.

      PERFORM field_attributes USING 'STPRS'  'X'.

      PERFORM field_attributes USING 'VERPR'  'X'.

      PERFORM field_attributes USING 'VPRSV'  'X'.

      PERFORM field_attributes USING 'PRFRQ'  'X'.

      PERFORM field_attributes USING 'MVGR2'  'X'.

      PERFORM field_attributes USING 'EXTWG'  'X'.

      PERFORM field_attributes USING 'VTWEG'  'X'.

      PERFORM field_attributes USING 'VKORG'  'X'.

      PERFORM field_attributes USING 'SPART'  'X'.

      PERFORM field_attributes USING 'BKLAS'  'X'.

      PERFORM field_attributes USING 'MATKL'  'X'.

      PERFORM field_attributes USING 'MSEHI'  'X'.

      PERFORM field_attributes USING 'MTART'  'X'.

      PERFORM field_attributes USING 'LGORT'  'X'.

      PERFORM field_attributes USING 'WERKS'  'X'.

      PERFORM field_attributes USING  'LONGTX'  'X'.

      PERFORM field_attributes USING  'MAKTX'   'X'.

      PERFORM field_attributes USING 'ZMCDAT'  'X'.

      MODIFY it_makt FROM wa_makt.

    ENDIF.

  ENDLOOP.

ENDFORM.                   " DISPLAY_ALV

*&---------------------------------------------------------------------*

*&      Form  FIELD_ATTRIBUTES

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*      -->P_0952   text

*      -->P_0953   text

*----------------------------------------------------------------------*

FORM field_attributes  USING    p_field

                                p_name.

  DATA ls_stylerow TYPE lvc_s_styl.

  DATA lt_styletab TYPE lvc_t_styl.

  ls_stylerow-fieldname = p_field.

  ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled.

  INSERT ls_stylerow  INTO TABLE wa_makt-field_style.

  CLEAR: ls_stylerow.

*  *******

ENDFORM.   

Former Member
0 Kudos

Hi priya,

when build a field catlog assign "X" to particular one column

gs_fieldcat-input = 'X'.

gs_fieldcat-edit = 'X'.

my code assign three filed catlog in 2 filed is editable filed:

i1 = i1 + 1.

PERFORM fieldcat USING i1 'GT_SMY_ALV' 'DEVELOPED_BY'  text-006  '25'  ''  ''  ''  '' '' '' '' ''.

i1 = i1 + 1.

PERFORM fieldcat USING i1 'GT_SMY_ALV' 'PROJECT'  text-008  '30'  ''  ''  'X'  'X' '' '' '' ''.

i1 = i1 + 1.

PERFORM fieldcat USING i1 'GT_SMY_ALV' 'PROJECT_MANAGER'  text-009  '30'  ''  ''  ''  '' '' '' '' ''.

Am using Form and perform easy to assign a filedcatlog

FORM fieldcat USING value(col_pos)

                     value(tabname)

                     value(fieldname)

                     value(reptext)

                     value(len)

                     value(no_out)

                     value(sum)

                     value(nozero)

                     value(edit)

                     value(no_chk)

                     value(dtype)

                     value(tdropdown)

                     value(tfieldname).

   gs_fieldcat-col_pos = col_pos.

   gs_fieldcat-fieldname = fieldname.

   gs_fieldcat-tabname = tabname.

   gs_fieldcat-reptext_ddic = reptext.

   gs_fieldcat-outputlen = len.

   gs_fieldcat-no_out = no_out.

   gs_fieldcat-do_sum = sum.

   gs_fieldcat-input = nozero.

   gs_fieldcat-edit = edit.

   gs_fieldcat-checkbox = no_chk.

   gs_fieldcat-datatype = dtype.

   gs_fieldcat-ref_tabname = tdropdown.

   gs_fieldcat-ref_fieldname = tfieldname.

   APPEND gs_fieldcat TO gt_fieldcat.

   CLEAR gs_fieldcat.

ENDFORM.   

Regards,

Viswanathan S.