2013-12-09 99 views
0

我有一個綁定到DataTable的標準DevExpress MVCxGridView,它只是一堆布爾值,第一列「SS」是一個字符串代碼,它是數據鍵。我遍歷所有列並動態創建gridview列。顯示的網格是一堆複選框,它們是可以配置的選項。將數據屬性添加到DevExpress MVC gridview單元格

我有一個jquery js文件,需要爲這些單元格設置data- *屬性才能注入必要的功能。我想知道如何爲每個TD單元添加「data- *」屬性。 「data-ss」是第一列中的數據鍵,而「data-wm」是列中的工作模式。

我的Razor視圖代碼如下:

@model System.Data.DataTable 
@{ 
    var gv = Html.DevExpress().GridView(
     settings => 
     { 
      settings.Name = "gv"; 
      settings.Enabled = true; 
      settings.KeyFieldName = "SS"; 
      settings.CallbackRouteValues = new { Controller = "Test", Action = "DataBindingPartial" }; 
      settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Auto; 
      settings.Settings.VerticalScrollBarMode = ScrollBarMode.Auto; 
      settings.Settings.VerticalScrollableHeight = 200; 
      settings.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowAllRecords; 

      MVCxGridViewBandColumn currentBand = null; 

      foreach (System.Data.DataColumn c in Model.Columns) 
      { 
       if (c.ColumnName == "SS") 
       { 
        DevExpress.Web.ASPxGridView.GridViewColumn column = settings.Columns.Add(c.ColumnName); 
        column.Caption = "SS"; 
        column.CellStyle.CssClass = "ss_head"; 
        column.HeaderStyle.CssClass = "ss_head_caption"; 
        column.HeaderStyle.Cursor = "pointer"; 
       } 
       else 
       { 
        // Get Column Definition retreives information based on the column name 
        // definition.ActivityType = "act" if activity or "dg" if DataGathering 
        // definition.WorkMode = abbreviated name of activity 
        // definition.Description = long description of activity 
        var definition = 
         TestModel.DefinitionColumn.GetColumnDefinition(c.ColumnName); 

        if (currentBand == null || currentBand.Name != definition.ActivityType) 
        { 
         currentBand = settings.Columns.AddBand(); 

         currentBand.Name = definition.ActivityType; 
         currentBand.Caption = definition.ActivityType == "act" ? "Activity" : "Data Gathering"; 
         currentBand.HeaderStyle.CssClass = String.Format("workmode_col workmode_{0}", definition.ActivityType); 
        } 
        DevExpress.Web.ASPxGridView.GridViewColumn column =      
          currentBand.Columns.Add(c.ColumnName, MVCxGridViewColumnType.CheckBox); 
        column.Caption = definition.WorkMode; 
        column.ToolTip = definition.Description; 
        column.Visible = true; 
        column.HeaderStyle.Cursor = "pointer"; 
        column.CellStyle.CssClass = String.Format("workmode_{0} workmode_selectable workmode_col", definition.ActivityType); 
        column.HeaderStyle.CssClass = String.Format("workmode_{0} workmode_col", definition.ActivityType); 
        column.Width = 35; 
       } 
      } 
     }); 
    var gvBound = gv.Bind(Model); 
    gvBound.Render(); 
} 

謝謝米哈伊爾。

使用這個我可以添加一個設置的配置來設置數據 - *屬性:

 settings.HtmlDataCellPrepared = (sender, e) => 
     { 
      e.Cell.Attributes.Add(
       "data-wm", 
       e.DataColumn.Caption 
      ); 
      e.Cell.Attributes.Add(
       "data-ssco", 
       e.KeyValue.ToString() 
      ); 
     }; 
+0

爲什麼不直接聯繫DevExpress支持? – Mikhail

回答

1

它可以使用GridViewSettings.HtmlDataCellPrepared事件來分配所需要的屬性。檢查this SO線程。

相關問題