若用ALV输出的数据,希望某个字段某些行中可以修改,某些行中不可以修改。我们可以利用ALV的单元格编辑状态对象(lvc_t_styl)来解决。
在数据存储的内表中增加如下的字段:
data:begin of gs_show occurs 0,
…………
cellstyle type lvc_t_styl,
end of gs_show.
假设我们希望主数据中的 CHKBX(选中)字段为可写,并且是复选框,那么我们需要在 lvc_t_fcat 中添加CHKBX 时,设定几个属性:
gt_fieldcat-edit = 'X' .
gt_fieldcat-checkbox = 'X'.
假设我们希望ECPNUM 为 某几个特定值的时候,CHKBX 不可修改,那么我们在输出数据到ALV时,需要循环主数据作如下的设定:
data ls_stylerow type lvc_s_styl.
loop at gs_show.
…………
data lt_styletab type lvc_t_styl.
if gs_show-ecpnum = '100000000126' or
gs_show-ecpnum = '0100000000129' or
gs_show-ecpnum = '100000000133' or
gs_show-ecpnum = '100000000144' .
ls_stylerow-fieldname = 'CHKBX'.
ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled.
insert ls_stylerow into table lt_styletab.
gs_show-cellstyle = lt_styletab.
endif.
modify gs_show.
endloop.
还需要在 lvc_s_layo 中设定单元格格式字段名:
ps_layout-stylefname = 'CELLSTYLE'.
最后输出的效果截图如下:
此例子是在 OO ALV ( cl_gui_alv_grid )中测试的。普通ALV中未做测试。