title: MVC Dynamic data Webgrid
body: <p>I'm trying to display pivoted data in a webgrid, with tidy headers and some sort of either a checkbox that changes the data, or a URL in the data that is clickable</p>
  
  <p>I used this link as a start</p>
  
  <p><a href="https://stackoverflow.com/questions/23297979/parsing-dynamic-sql-in-c-sharp-and-binding-to-webgrid-in-asp-net-mvc">Parsing Dynamic SQL in C# and binding to WebGrid in ASP.Net MVC</a></p>
  
  <p>And I'm very nearly there, but I can't seem to get the data in the table as well as create a link. I have both in two columns and I want one column</p>
  
  <p>here's my controller</p>
  
  <pre><code>public ActionResult GridIndex()
      {
          StoreDepartmentBusinessLayer sdc = new StoreDepartmentBusinessLayer();
          List&lt;dynamic&gt; PivotList = sdc.GetData();
  
          List&lt;WebGridColumn&gt; columns = new List&lt;WebGridColumn&gt;();
          columns.Add(new WebGridColumn() { ColumnName = "SapStoreID", Header = "Id",CanSort=true });
          columns.Add(new WebGridColumn() { ColumnName = "StoreName", Header = "Store", CanSort = true, Style = "webgrid-column-storename" });
  
          //List&lt;Department&gt; Departments = sdc.StoreDepartments.Select(x=&gt;x.DeptID + " " + x.DeptName).Distinct().ToList();
  
          DepartmentBusinessLayer dbl = new DepartmentBusinessLayer();
  
          List&lt;Department&gt; Departments = dbl.Departments.Where(x =&gt; x.Active==1 &amp;&amp; x.Calculation==0).ToList();
  
          foreach (Department LoopDept in Departments)
          {
              //columns.Add(new WebGridColumn() { ColumnName = LoopDept.DeptID.ToString(), Header = LoopDept.DeptName});
              char[] DeptVertical = LoopDept.DeptName.ToCharArray();
              string DeptVerticalString = string.Join(Environment.NewLine, DeptVertical);
              //this one works ok for 0 and 1
              columns.Add(new WebGridColumn() { ColumnName = LoopDept.DeptID.ToString(), Header = DeptVerticalString, Style = "webgrid-column-tick" });
  
              columns.Add(new WebGridColumn()
              {
                  ColumnName = LoopDept.DeptID.ToString() + "_Link",
                  Header = "",
                  Style = "webgrid-column-tick",
                  Format = (item) =&gt;
                  {
                      return new HtmlString(string.Format("&lt;a href= {0}&gt;{1}&lt;/a&gt;",
                          Url.Action("Edit", "Edit", new { PseudoKey = item.SapStoreID + "_" + LoopDept.DeptID.ToString() }), "*"));
                  }
              });
  
          ViewBag.Columns = columns;
  
          return View(PivotList);
      }
  </code></pre>
  
  <p>and here's what I've done in the view</p>
  
  <pre><code>@{
      var grid = new WebGrid(Model, canPage: true, rowsPerPage: 100,
      selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
      grid.Pager(WebGridPagerModes.NextPrevious);
      }
  @grid.GetHtml(tableStyle: "webgrid-tidy", headerStyle: "webgrid-header", columns: ViewBag.Columns,rowStyle: "webgrid-row-style",alternatingRowStyle: "webgrid-alternating-row")
  </code></pre>
  
  <p>and that runs ok, but there are two columns where i'd like one, with the value and the link</p>
  
  <p>I'm a bit of a MVC novice and I appreciate that this is fiddly and I'm out of my depth a bit, but any advice is welcome</p>
  
  <p>My specific question is...I can't get the Link column to populate with item.Value (I really want to put "item.Value" instead of "*" as that seems right) and I think is because it's ExpandoObject, I'm not sure how to pull out the value it's populating with</p>
  
  <p>regards</p>
  
  <p>ol</p>