1

I have dynamic column and write dynamic style but I receive an error:

CS1977 C# Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

View:

   WebGrid grid = new WebGrid(Model);
    List<WebGridColumn> columnsL = new List<WebGridColumn>();

    for (int i = 0; i < Model.First().Data.Count; i++)
    {
        int local = i;
        columnsL.Add(grid.Column(Model.First().Data[i].Name, Model.First().Data[i].Label,format: item => new MvcHtmlString("<text>" + item.Data[local].Value +"</text>")
    ));
    }


@grid.GetHtml(tableStyle: "table table-striped table-bordered", columns: columnsL)
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
bgmm
  • 15
  • 1
  • 8
  • 9
    why would you write a line of code like that? That's hideous. ["Any fool can write code that a computer can understand. Good programmers write code that humans can understand"](http://stackoverflow.com/a/522907/542251) – Liam Oct 06 '15 at 14:56
  • Because I am writing for framework,I must write dynamic. – bgmm Oct 08 '15 at 06:50
  • It's perfectly possible to write dynamic code that is readable. – Liam Oct 08 '15 at 07:54

1 Answers1

0

I found the solution !If you want dynamic WebGrid in ASP.NET MVC 5 ,you can use this code in View

     @{
            WebGrid grid = new WebGrid(Model);
            List<WebGridColumn> columnsL = new List<WebGridColumn>();
    foreach (ServiceReference1.Column col in ViewBag.ColumnList)
            {
                WebGridColumn c = new WebGridColumn();
                c.ColumnName = col.columnName;
                c.Header = col.columnCaptionValue;
                c.Format = (item) => @Html.Raw("<div style='word-wrap:hyphenate;height:30px;min-width:" + col.columnWidth + "px'><b style='background-color:palevioletred'>" + item[col.columnName] + "</b></div>");

                columnsL.Add(c);
            }
}
    <div class="test" style="overflow: scroll;">
            <div style="width:1000px"> 
        @grid.GetHtml(tableStyle: "table table-striped table-bordered", columns: columnsL, caption: ViewBag.GridHeader, headerStyle: "gvHeading")
            </div>
        </div>

My problem was item but I solve item[col.columnName] .It now works

Liam
  • 27,717
  • 28
  • 128
  • 190
bgmm
  • 15
  • 1
  • 8