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

时间:2016-08-09 05:16:13

标签: asp.net-mvc asp.net-mvc-4 devexpress devexpress-mvc mvcgrid.net

我有一个mvc devexpress网格,我想要实现一个功能,当编辑网格时,其中一个列必须是readonly而其他可更改。并且在添加新行时,所有columsn都必须是可更改的包括之前所说的readonly专栏。

这是我的网格代码

@{

    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

任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

这与你的预览非常相似:

Set ReadOnly property to a column of devexpress grid(MVC) only on edit click

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

    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()
            );
        });
    });