ASPxGridView - 如何在编辑行时将某些字段编辑器设为只读,并在添加新行时允许其编辑

Posted

技术标签:

【中文标题】ASPxGridView - 如何在编辑行时将某些字段编辑器设为只读,并在添加新行时允许其编辑【英文标题】:ASPxGridView - How to make some field editors read-only when editing a row and allow their editing when adding a new row 【发布时间】:2016-09-30 12:33:34 【问题描述】:

我在这里关注它,https://www.devexpress.com/Support/Center/Example/Details/E5116 并在 ASP.NET MVC 中应用了给定的示例。以下是我的gridview设置示例部分代码:

settings.SettingsEditing.Mode = GridViewEditingMode.Batch;
settings.SettingsEditing.BatchEditSettings.EditMode = GridViewBatchEditMode.Row;
settings.CellEditorInitialize = (s, e) =>

    ASPxGridView grid = s as ASPxGridView;
    ASPxEdit editor = (ASPxEdit)e.Editor;
    if (e.Column.FieldName == "columnName1" || e.Column.FieldName == "columnName2")
    
        e.Editor.ReadOnly = !grid.IsNewRowEditing;
    
;

现在我无法在更新现有行数据的同时编辑两个单元格,这看起来很完美。

但我无法在使用新按钮创建新行时编辑两列。

注意:- 它在以下情况下工作:

 settings.SettingsEditing.Mode = GridViewEditingMode.Inline;

但在编辑模式为批处理时不起作用。

【问题讨论】:

如何将编辑器的可见性指定为 false 并在 CellEditorInitialize 中检查 grid.IsNewRowEditing 并相应地切换可见性。它对我有用! @GHKarim,听起来不错,会尝试让您知道。谢谢男人。 【参考方案1】:

最后我在 DevExpress 支持的帮助下实现了这一点:

部分视图中的网格设置如下:

@html.DevExpress().GridView(settings=> 
settings.Name = "gridViewPartial";
settings.CallbackRouteValues = new  Controller = "Home", Action = "gridViewPartial" ;
settings.SettingsEditing.BatchUpdateRouteValues = new  Controller = "Admin", Action = "gridViewBatchUpdate" ;

settings.KeyFieldName = "Id";

settings.SettingsEditing.Mode = GridViewEditingMode.Batch;
settings.SettingsEditing.BatchEditSettings.EditMode = GridViewBatchEditMode.Cell;


//to handle edit events on client side
**settings.ClientSideEvents.BatchEditStartEditing = "OnBatchStartEdit";**

settings.CommandColumn.Visible = true;
settings.CommandColumn.ShowDeleteButton = true;
settings.CommandColumn.ShowNewButtonInHeader = true;


settings.Columns.Add(column =>

    column.FieldName = "Id";
    column.Caption = "ID";
    column.ColumnType = MVCxGridViewColumnType.SpinEdit;

);
settings.Columns.Add(column =>

    column.FieldName = "Name";
    column.Caption = "Name";

);

).Bind(Model).GetHtml()

我在其中引用部分视图的网格普通页面:

//script to handle edit event
function OnBatchStartEdit(s, e) 
    if ((e.focusedColumn.fieldName == "Id") && (e.visibleIndex > -1)) //cancel example
        e.cancel = true;;

供进一步参考 访问这里https://www.devexpress.com/Support/Center/Question/Details/T386483

【讨论】:

以上是关于ASPxGridView - 如何在编辑行时将某些字段编辑器设为只读,并在添加新行时允许其编辑的主要内容,如果未能解决你的问题,请参考以下文章

如何在DevExpress ASPxGridView中进行编辑

如何对DevExpress ASPxGridView进行分组排序

如何将我的 excel gridview 添加到我的 aspxgridview 并保存在 sql server 中?

使用复合 KeyFieldName 获取 ASPxGridView.RowUpdating 事件中的更新行

aspxgridview如何隐藏列?急!在线等!

怎样向ASPxGridView中的某一个单元格添加值!