title: MVC WebGrid Dynamic Columns
body: <p>So I have a model that contains a list of meta data items that is customizable per user.</p>
  
  <pre><code>public class MyModel {
      public int ID { get; set; }
      public String Name { get; set; }
      public DateTime Created { get; set; }
  
      // Need to diplay each as a column
      public List&lt;MetaData&gt; Data { get; set; }
  }
  public class MetaData {
      public int ID { get; set; }
      public String Name { get; set; }
      public String Label { get; set; }
      public String Value { get; set; }
  }
  </code></pre>
  
  <p>For each metadata item on MyModel I need to add a column to the WebGrid that has the name as the column name, the label as the header, and the value outputted for each MyModel.</p>
  
  <pre><code>@{
      WebGrid grid = new WebGrid(source: Model, rowsPerPage: 5, canPage: true);
  
      List&lt;WebGridColumn&gt; cols = new List&lt;WebGridColumn&gt;();
      cols.Add(grid.Column("Name", "Name"));
      cols.Add(grid.Column("Created"));
      for (int i = 0; i &lt; Model.FirstOrDefault().Data.Count; i++)
      {
          cols.Add(grid.Column(Model.FirstOrDefault().Data[i].Name, Model.FirstOrDefault().Data[i].Label,
              format: item =&gt; @&lt;text&gt;@item.Data[i].Value&lt;/text&gt;
          ));
      }
  }
  
  @grid.GetHtml(
      tableStyle: "table",
      htmlAttributes: new { id = "MyGrid" },
      columns: cols)
  </code></pre>
  
  <p>The metadata columns are shared between all MyModels so each one will not have a new set of columns. The name and label will be the same throughout the different MyModel's. If anyone can help, it would be much appreciated. </p>