2013-03-08 2 views
1

아래 코드에서와 같이 배열 이름에서 내 검도 표제를 만드는 데 어려움이 있습니다. columnName은 내 열/필드 이름으로 사용하려는 배열입니다.Kendo UI 그리드를 동적으로 작성하십시오.

@(Html.Kendo() 
    .Grid<rtxVending.Web.Models.ProductDetails>() 
    .Name("ProductDetailGrid").ClientDetailTemplateId("") 
    .HtmlAttributes(new { @style = "align:center; font-size:9px;" }) 
    .Columns(columns => 
    { 
     var colums = rtxVending.Web.Repositories.SessionRepository.GetSessionObject<IList<rtxVending.Web.Models. 
     ProductCategoryTags>>(ApplicationConstants.sesProductsHeaderCategoryTags); 
     if (colums != null && colums.Count > 0) 
     { 
      **//columnName is an array that i want to use as my column/field name** 
      var columnName = colums.Select(a => a.ValueX).ToArray(); 

      foreach (var column in columnName) 
      { 
       columns.Bound(column.ToString()); 
      } 
     } 
     else 
     { 
      columns.Bound(o => o.Amount).Width(100); 
      columns.Bound(o => o.Value).Width(100); 
      columns.Bound(o => o.Value1).Width(100); 
      columns.Bound(o => o.Value2).Width(100); 
      columns.Bound(o => o.Value3).Width(100); 
      columns.Bound(o => o.Value4).Width(100); 
      columns.Bound(o => o.Value5).Width(100); 
      columns.Bound(o => o.Value6).Width(100); 
      columns.Bound(o => o.Value7).Width(100); 
      columns.Bound(o => o.Value8).Width(100); 
      columns.Bound(o => o.Value9).Width(100); 
      columns.Bound(o => o.Value10).Width(100); 
     } 
    }) 
    .Pageable(pager => pager.Refresh(true)) 
    .Sortable() 
    .Scrollable() 
    .DataSource(dataSource => dataSource 
     .Ajax() 
     .Read(read => read.Action("GetProductDetailsGrid", "Products")) 
     .Events(events => events.Error("error_handler")) 
    ) 
) 

답변

2

피클 교수님의 답변에 감사드립니다.

이것은 내가 한 것입니다. 그리드 데이터를 읽는 방법은 아래와 같습니다. 보기가 나오면 내 세션이보기에 아무것도 통과하지 다른 null가 아닌 경우, 내가 그리드에 데이터를 전달하기 전에 다음과 같이

public ActionResult GetProductDetailsGrid([DataSourceRequest] DataSourceRequest request) 
    { 
     try 
     { 
      List<ProductDetails> productDetails = null; 
      //IEnumerable<ProductDetails> productDetails = null; 

      if (SessionRepository.VerifySessionExists(ApplicationConstants.sesProductDetails)) 
      { 
       productDetails = SessionRepository.GetSessionObject<List<ProductDetails>>(ApplicationConstants.sesProductDetails).ToList(); //.AsEnumerable(); 

       //Return converted list<T> as DataTable 
       var dataTable = ToDataTable<ProductDetails>(productDetails); 
       if (dataTable != null) 
       { 
        var dataColumnCollection = dataTable.Columns; 
        SessionRepository.CacheSession<DataColumnCollection>(ApplicationConstants.sesAcquireStockDataColumnCollection, dataColumnCollection); 
       } 

       return Json(dataTable.ToDataSourceResult(request)); 
      } 
     } 
     catch (Exception ex) 
     { 
      ModelState.AddModelError("", ex.Message); 
      return Json(ModelState.ToDataSourceResult()); 
     } 



     return Json(ModelState.ToDataSourceResult(request)); 
    } 

내 모델은

public class ProductDetails 
{ 
     [Key,ScaffoldColumn(false)] 
     public long ProductDetailId { get; set; } 
     public int ProductHeaderId { get; set; } 
     public double Amount { get; set; } 
     public string Value { get; set; } 
     public string Value1 { get; set; } 
     public string Value2 { get; set; } 
     public string Value3 { get; set; } 
     public string Value4 { get; set; } 
     public string Value5 { get; set; } 
     public string Value6 { get; set; } 
     public string Value7 { get; set; } 
     public string Value8 { get; set; } 
     public string Value9 { get; set; } 
     public string Value10 { get; set; } 
     public string Value1DisplayName {get; set;} 
     public bool Valid { get; set; } 

} 

입니다 :

public ActionResult AcquireStock() 
    { 
     //TODO uncomment the Session Below 
     //SessionRepository.RemoveSession(ApplicationConstants.sesProductDetails); 

     var productHeader = new ProductHeaders(); 
     if (SessionRepository.VerifySessionExists(ApplicationConstants.sesProductDetails)) 
     { 


      var productDetails = SessionRepository.GetSessionObject<List<ProductDetails>>(ApplicationConstants.sesProductDetails).ToList(); //.AsEnumerable(); 

      //Return converted list<T> as DataTable 
      var dataTable = ToDataTable<ProductDetails>(productDetails); 
      if (dataTable != null) 
      { 
       productHeader.ProductDetailsDataTable = dataTable; 
       //SessionRepository.CacheSession<DataColumnCollection>(ApplicationConstants.sesAcquireStockDataColumnCollection, dataColumnCollection); 
      } 

      return View(productHeader); 
     } 

     productHeader.ProductDetailsDataTable = null; 

     return View(productHeader); 
    } 

나는 모든 데이터를 표시하지 않고도 열 이름과 행을 각각 전달하여 데이터를 데이터 테이블로 변환하는 일반적인 방법을 만들었습니다 (). 모델 특성 값. ""또는 null이 아닌 값을 사용하여 필드를 전달하려고합니다. 그리드에 표시됩니다. 다음과 같이 내 일반적인 방법은 : 내 그리드 지금

//Converts Generic List to DataTable 
    private DataTable ToDataTable<T>(List<T> data)// T is any generic type 
    { 
     PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); 
     int columnCount = 0; 
     DataTable table = new DataTable(); 
     var colums = rtxVending.Web.Repositories.SessionRepository.GetSessionObject<IList<rtxVending.Web.Models.ProductCategoryTags>>(ApplicationConstants.sesProductsHeaderCategoryTags); 
     if (colums != null && colums.Count > 0) 
     { 
      var columnName = colums.Select(a => a.ValueX).ToArray(); 

      for (int i = 0; i < columnName.Count(); i++) 
      { 
       table.Columns.Add(columnName[i]); 
      } 

      columnCount = columnName.Count(); 
     } 
     else 
     { 
      for (int i = 0; i < props.Count; i++) 
      { 
       PropertyDescriptor prop = props[i]; 
       table.Columns.Add(prop.Name, prop.PropertyType); 
      }    
     }    

     object[] values = new object[props.Count]; 
     foreach (T item in data) 
     { 
      for (int i = 0; i < values.Length; i++) 
      { 
       values[i] = props[i].GetValue(item); 
      } 

      object[] newValues = new object[columnCount]; 
      int j = 0; 
      foreach (var p in values) 
      { 
       Type argType = p.GetType(); 

       if (argType == typeof(bool) && !((bool)p)) 
       { 
        newValues[j] = p; 
        j++; 
       } 
       else if(argType == typeof(int) && (int)p != 0) 
       { 
        newValues[j] = p; 
        j++; 
       } 
       else if (argType == typeof(double) && (double)p != 0) 
       { 
        newValues[j] = p; 
        j++; 
       } 
       else if (argType == typeof(string) && p != null && p != string.Empty) 
       { 
        newValues[j] = p; 
        j++; 
       } 

       if (j >= columnCount) 
        break; 
      } 

      //table.Rows.Add(values); 
      //table.Rows.Add(newValues); 
     } 
     return table; 
    } 

입니다

@(Html.Kendo() 
     //.Grid<rtxVending.Web.Models.ProductDetails>() 
        .Grid(Model.ProductDetailsDataTable) 
        .Name("ProductDetailGrid")//.ClientDetailTemplateId("") 
        .HtmlAttributes(new { @style = "align:center; font-size:9px;" }) 
        .Columns(columns => 
        { 
         //var columnData = rtxVending.Web.Repositories.SessionRepository.GetSessionObject<System.Data.DataColumnCollection>(ApplicationConstants.sesAcquireStockDataColumnCollection); 

         //if(columnData != null && columnData.Count > 0) 
         if (Model.ProductDetailsDataTable != null) 
         foreach (System.Data.DataColumn column in Model.ProductDetailsDataTable.Columns) 
         { 
          columns.Bound(column.ColumnName); 
         } 
}) 
.Pageable(pager => pager.Refresh(true)) 
.Sortable() 
.Scrollable()  
.DataSource(dataSource => dataSource 
    .Ajax() 
    .Read(read => read.Action("GetProductDetailsGrid", "Products")) 
         .Model(model => 
        { 
         if (Model.ProductDetailsDataTable != null) 
         foreach (System.Data.DataColumn column in Model.ProductDetailsDataTable.Columns) 
         { 
          model.Field(column.ColumnName, column.DataType); 
         }     
        }) 

.Events (이벤트 => events.Error ("error_handler"))) )

하는 경우가 내가 설명 할 필요가있는 어떤 것이라도, 나는 가능한 빨리 대답 할 것이다. 피클 교수님 감사합니다.

관련 문제