MVC grid view for variable columns

Apr 28, 2016 at 8:51 PM
want to display a set of data with multiple columns as years. The columns are variable and can change based on data retrieved from database. following is snippet:
------------class---------
public class gridmatx
{  public Grid Grid { get; set; }
}
public class Grid
{  public List<string> Columns { get; set; }
    public List<GridRow> Rows { get; set; }
}
public class GridRow
{  public string Label { get; set; }
    public List<decimal> Values { get; set; }
    public List<GridRow> ChildRows { get; set; }
}
---------Controller--------------
   public ActionResult Index1()
    {   gridmatx g = new gridmatx();
        g.Grid = new Grid();
        g.Grid.Columns = new List<string> { "2011", "2012", "2013", "2014", "2015" };
        g.Grid.Rows = new List<GridRow>
        {   new GridRow { Label = "First", Values = new List<decimal> { 5M, 2M, 1M, 8M, 10M } },
            new GridRow { Label = "Second", Values = new List<decimal> { 2M, 2M, 4M, 8M, 9M } },
            new GridRow { Label = "Third", Values = new List<decimal> { 12M, 8M, 1M, 9M, 3M } }
        };
        return View(g);
   }
------------View---------
@model myproj.Models.gridmatx
@using GridMvc.Html
@{
ViewBag.Title = "Grid data ";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<meta name="viewport" content="width=device-width" />
<link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
<link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>
</head>
<body>
<h1>Grid.MVC</h1>
@Html.Grid(Model.Grid.Rows).Columns(columns =>
{   columns.Add(r => r.Label).Titled("Type");
    for (int i = 0; i < Model.Grid.Columns.Count; i++)
    {   var index = i;
        string value1 = Model.Grid.Columns[i];
        columns.Add(r => r.Values.ToArray()).Titled(value1);
    }
  }  )
 </body>
 </html>
using r => r.Values.ToArray() causes error. I am not sure how to list the variable columns here. I used the example from the link: https://gridmvc.codeplex.com/wikipage?title=Custom%20columns used for telerik.

Error:
An exception of type 'System.NotSupportedException' occurred in GridMvc.dll but was not handled in user code Additional information: Expression 'r => r.Values.ToArray()' not supported by grid

If I use the following:
columns.Add(r => r.Values[I]).Titled(value1); 
I get the Error:
"Expression 'r => r.Values.get_Item(value(ASP._Page_Views_viewfolder_Index1_cshtml+<>c__DisplayClas‌​s3_0).i)' not supported by grid"

I will appreciate any help me correct error to display variable columns in "Values".
Apr 29, 2016 at 1:46 PM
Edited Apr 29, 2016 at 1:47 PM
Hi!

you're trying something like this? Image

if so, then adjust your code to use "RenderValueAs".
@Html.Grid(Model.Grid.Rows).Columns(columns =>
{
    columns.Add(r => r.Label).Titled("Type");
    for (int i = 0; i < Model.Grid.Columns.Count; i++)
    {
        var index = i;
        var title = Model.Grid.Columns[i];
        columns.Add().RenderValueAs(r=> r.Values[index].ToString()).Titled(title);
    }
})
Marked as answer by visual_thread on 4/29/2016 at 6:55 AM
Apr 29, 2016 at 1:55 PM
Thanks! That worked.