2016-08-08 93 views
0

設置只讀屬性的DevExpress網格(MVC)的專欄中,我有一個網格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() 

這是我網格的樣子初始加載。 enter image description here

和更新點擊編輯 enter image description here

回答

0

如果我理解你很好的時候,我想你會需要使用編輯模板來解決你所描述的問題。

裏面編輯模板操作,您可以檢查是否有新行處於編輯通過檢查屬性值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

+0

我試過了,才發現整個Datecontrol的列disaap耳朵,而不是隻使其只讀@peters –

+0

它消失了,因爲這只是一個例子。您需要在'if'語句中添加特定的Devexpress MVC控件定義。這會將HTML控制輸出寫入ViewContext.Writer。是這樣的: '代碼 ViewContext.Writer.Write( Html.DevExpress()DateEdit(settingsDateEdit => { settingsDateEdit.Name = 「FROM日期」; settingsDateEdit.Width = Unit.Percentage(100)。 ; settingsDateEdit.ReadOnly =!e.Grid.IsNewRowEditing; })。Bind(fromDate).GetHtml() ); ' – PeterS

+0

請試試這個,讓我知道它是否適合你。如果有什麼不清楚的,請讓我知道:) – PeterS