在特定列而不是整个网格上使用 settings.CellEditorInitialize

Use settings.CellEditorInitialize on a particular column instead of entire grid

我有一个 mvc devexpress 网格,我想实现一个功能,在编辑网格时,其中一列必须是只读的,而其他 changeable.And 添加新行时,所有列必须是可变的,包括前面提到的只读列。

这是我的网格代码

@{

    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "GridViewDuration";
        settings.CallbackRouteValues = new { Controller = "DurationMaster", Action = "GridViewDurationPartial" };

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "MyController", Action = "MyControllerAction1" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "MyController", Action = "MyControllerAction2" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "MyController", Action = "MyControllerAction3" };
        settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
        settings.SettingsBehavior.ConfirmDelete = true;

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.ShowNewButton = true;
        settings.CommandColumn.ShowDeleteButton = true;
        settings.CommandColumn.ShowEditButton = true;

        settings.KeyFieldName = "DurationId";

        settings.SettingsPager.Visible = true;
        settings.SettingsPager.PageSize = 20;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = false;
        settings.Columns.Add(column =>
        {
            column.FieldName = "Column1";
            column.ReadOnly = true;
            column.Width = 20;

        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "Column2";
            column.Caption = "xyz";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            column.Width = 250;

            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            comboBoxProperties.DataSource = PMC.Web.Controllers.DurationMasterController.getSelectList("0", "MyAction4");
            comboBoxProperties.TextField = "Text";
            comboBoxProperties.ValueField = "Value";
            comboBoxProperties.ValueType = typeof(int);
            comboBoxProperties.ValidationSettings.RequiredField.IsRequired = true;


        });
        settings.InitNewRow = (sender, e) =>
        {
            e.NewValues["ColumnId"] = 0;
        };
        settings.Columns.Add("ColumnName");
        settings.Columns.Add("Description");
        //settings.Columns.Add("DisplayName");
        settings.Columns.Add(column =>
    {
        column.Caption = "FromDate";
        column.FieldName = "FromDate";
        column.ColumnType = MVCxGridViewColumnType.DateEdit;
        column.ReadOnly = true;
        var DateEditProperties = column.PropertiesEdit as DateEditProperties;
        column.CellStyle.Wrap = DefaultBoolean.False;
    });



    settings.Columns.Add(column =>
    {
        column.Caption = "ToDate";
        column.FieldName = "ToDate";

        column.ColumnType = MVCxGridViewColumnType.DateEdit;
        var DateEditProperties = column.PropertiesEdit as DateEditProperties;
        column.CellStyle.Wrap = DefaultBoolean.False;
    });
    });
    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }

}
@grid.Bind(Model).GetHtml()  

这就是我尝试过的

settings.CellEditorInitialize += (s, e) =>
        {
            var abc = s as ASPxGridView;
            e.Editor.ReadOnly = !abc.IsNewRowEditing;
        };

这会将所有网格列设置为只读,而我只需要 fromDate 列为 readonly

如有任何帮助,我们将不胜感激。谢谢。

我仍然认为您应该在该特定列上设置操作 MVCxGridViewColumn.SetEditItemTemplateContent

这个问题与您的预览非常相似:

请尝试以下解决方案,它应该有效:

    settings.Columns.Add(col =>
    {
        col.FieldName = "FromDate";
        col.Caption = "From Date";
        col.Width = Unit.Percentage(15);
        col.ColumnType = MVCxGridViewColumnType.DateEdit;

        col.SetEditItemTemplateContent(e =>
        {
            var fromDate = DateTime.Now;

            ViewContext.Writer.Write(
                Html.DevExpress().DateEdit(settingsDateEdit =>
                {
                    settingsDateEdit.Name = "fromDate";
                    settingsDateEdit.Width = Unit.Percentage(100);

                    settingsDateEdit.ReadOnly = !e.Grid.IsNewRowEditing;

                }).Bind(fromDate).GetHtml()
            );
        });
    });

相反,您可以在所需列上使用 CellEditorInitialize 事件

settings.CellEditorInitialize += (s, e) =>
{
    var abc = s as ASPxGridView;
    if (e.Column.FieldName == "YourFieldName")
        e.Editor.ReadOnly = !abc.IsNewRowEditing;
};