仅在编辑单击时将 ReadOnly 属性设置为 devexpress 网格(MVC)的列

Posted

技术标签:

【中文标题】仅在编辑单击时将 ReadOnly 属性设置为 devexpress 网格(MVC)的列【英文标题】:Set ReadOnly property to a column of devexpress grid(MVC) only on edit click 【发布时间】:2016-12-14 00:31:48 【问题描述】:

我的局部视图中有一个 devexpress 网格,

网格的每一行都有一个添加、更新和删除按钮。

当点击特定行的更新时,会弹出一个表单,其中包含网格的所有列。

现在我想将其中一列的只读属性设置为 true,这是 column.ReadOnly = true; 所做的

但这所做的是将列始终设置为只读,即即使在新点击期间也是如此。

我只在编辑点击期间想要这个只读属性。

这是我的 devexpress 网格代码

@

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

这就是我的网格在初始加载时的样子。

当点击更新进行编辑时

【问题讨论】:

【参考方案1】:

如果我理解你,我认为你需要使用编辑模板来解决你描述的问题。

在编辑模板操作中,您可以通过检查属性值 ASPxGridView.IsNewRowEditing 来检查新行是否正在编辑中

请看下面的例子。

    settings.Columns.Add(col =>
    
        col.FieldName = "Id";
        col.Caption = "Id";

        col.SetEditItemTemplateContent(e =>
        
            if (e.Grid.IsNewRowEditing)
            
                // your code here
            
            else
            
                // your code here
            
        );
    );

请查看以下链接了解更多详情

MVCxGridViewColumn.SetEditItemTemplateContent https://documentation.devexpress.com/#AspNet/DevExpressWebMvcMVCxGridViewColumn_SetEditItemTemplateContenttopic(68Z8MA)

ASPxGridView.IsNewRowEditing https://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridView_IsNewRowEditingtopic

【讨论】:

我试过了,只是发现该列的整个 Datecontrol 都消失了,而不是仅仅将其设为只读 @peters 它消失了,因为这只是一个例子。您需要在“if”语句中添加特定的 Devexpress MVC 控件定义。这会将 HTML 控件输出写入 ViewContext.Writer。像这样的东西:code ViewContext.Writer.Write( Html.DevExpress().DateEdit(settingsDateEdit => settingsDateEdit.Name = "fromDate"; settingsDateEdit.Width = Unit.Percentage(100); settingsDateEdit.ReadOnly = !e.Grid.IsNewRowEditing; ).Bind(fromDate).GetHtml() ); 请试试这个,让我知道它是否适合你。如果有不清楚的地方也请告诉我:)

以上是关于仅在编辑单击时将 ReadOnly 属性设置为 devexpress 网格(MVC)的列的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript:readOnly属性未设置

仅在按钮单击时添加记录

c#怎么让richtextbox不可编辑?

textbox的enabled属性和readonly的区别

仅在单击完成按钮时将 UIPickerView 选择添加到 TextField

基础问题:设置radioselectcheckbox 的readonly 属性