title: MVC WebGrid Dynamic Content
body: <p>What I am trying to achieve is hard to explain, so please let me know if I can provide more information.  I have a feeling that I am trying to use WebGrids beyond their means, so if anyone has any open source alternatives that would do what I am trying, that would also be helpful.</p>
  
  <p>I need to build a WebGrid from the ground up, because the content returned to it (columns and values) changes depending on other criteria on my form.  I have this working in a few simple lines of code in a Table, but I wanted to use WebGrid for the styling, sorting and paging.</p>
  
  <pre><code>@model DocumentSearchViewModel
  
  @if ((this.Model != null) &amp;&amp; (this.Model.SearchResults != null) &amp;&amp;     (this.Model.SearchResults.Count() &gt; 0))
  { 
      &lt;table&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;Document&lt;/th&gt;
                  @foreach (var metadata in this.Model.SearchResults.Metadata)
                  {
                      &lt;th&gt;
                          @metadata.InstanceFieldName
                      &lt;/th&gt;
                  }
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              @foreach (var document in this.Model.SearchResults)
              {
                  &lt;tr&gt;
                  &lt;td&gt;
                      @Html.ActionLink(document.Id.ToString(), "Details", new { id = document.Id })
                  &lt;/td&gt;
                      @foreach (var metadata in document.Metadata)
                      {
                          &lt;td&gt;
                              @metadata.Value
                          &lt;/td&gt;
                      }
                  &lt;/tr&gt;
              }
          &lt;/tbody&gt;
      &lt;/table&gt;
  }
  </code></pre>
  
  <p>So to explain the model structure, The SearchResults has an <code>IEnumerable&lt;MetadataModel&gt;</code>, and this MetadataModel has properties of InstanceFieldName and Value.  When a search is performed, every result will always have the same number of Metadata, and the same InstanceFieldNames, but different values, however a quick change of a field on the form, and a new search could return a new set of results (Again with the same set of Metadata as each other, but different to the first set of results)</p>
  
  <p>The columns of the grid correspond to the Metadata InstanceFieldName, and the content the Value.</p>
  
  <p>So I have made my best attempt using WebGrid, but the best I can get is a grid with the right column headers, and the right number of rows, the columns have the right data, but each row is the same (copied from the last row).</p>
  
  <pre><code>@{
  var grid = new WebGrid(canPage: true, canSort: true, rowsPerPage: Model.PageSize,     sortFieldName: Model.Sort, sortDirectionFieldName: Model.SortDir);
  grid.Bind(Model.SearchResults, rowCount: Model.DocumentCount);
  List&lt;WebGridColumn&gt; cols = new List&lt;WebGridColumn&gt;();
  foreach(var metadata in Model.SearchResults.Select(r =&gt; r.Metadata).FirstOrDefault())
  {
      var col = new WebGridColumn();
      col.ColumnName = metadata.InstanceFieldName;
      col.Header = metadata.InstanceFieldHeader;
      col.Style = "gridRow";
      col.CanSort = true;
      cols.Add(col);
  }
  
  foreach (var result in Model.SearchResults)
  {        
      foreach (var col in cols)
      {
          var metadataValue = result.Metadata.Single(m =&gt; m.InstanceFieldName == col.ColumnName).Value;
          col.Format = (item) =&gt; @Html.Raw("&lt;text&gt;" + metadataValue + "&lt;/text&gt;");
      }
  }
  }
  
  @if ((this.Model != null) &amp;&amp; (this.Model.SearchResults != null) &amp;&amp; (this.Model.SearchResults.Count() &gt; 0))
  {   
      @grid.GetHtml(htmlAttributes: new { id = "documentGrid" }, rowStyle: "gridRow", alternatingRowStyle: "gridRowAlt", columns: cols)
  }
  </code></pre>
  
  <p>What I'm trying to work out is how and if possible to generate the columns first, and then populate the rows into these columns.</p>
  
  <p>Any help is really appreciated, thanks, Mark</p>