title: Create dynamically table with different col numbers on each row in ASP.NET MVC 3
body: <p>I have template forms that the user can fill with data. The forms are represented as tables. In the database the data for each filled template form is saved with RowNum and ColNum so I can recreate the form on demand. The problem is that for one form the columns on the different rows vary. Just to make it clear here is a simple example :</p>
  
  <p><img src="https://i.stack.imgur.com/C3h0w.png" alt="Example Table"></p>
  
  <p>Here the firs row is empty, the second has one column which display a picture and then follows 4 rows with two columns.</p>
  
  <p>This is just an example. Te set up will vary from form to form. Now I collect the data like this:</p>
  
  <pre><code>List&lt;object&gt; dummyData = new List&lt;object&gt;()
              {
                  new  {Row = 1, Col = 1, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
                  new  {Row = 1, Col = 2, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
                  new  {Row = 2, Col = 1, IsRequired = true, QuestionText = "No?", FieldValue = "string"},
                  new  {Row = 3, Col = 1, IsRequired = false, QuestionText = "No?", FieldValue = "string"}
              };
  ViewBag.Header = dummyData;
  </code></pre>
  
  <p>This is just for testing but the real data will look much like this. So as you can see the first and the second record are for the same row but right now I can't think of a way to represent the data correctly in the view. </p>
  
  <p>Right now I'm hard coding everything by using <code>&lt;td colspan="2"&gt;</code> where I know I don't need two columns and yet no solution about how to add extra column instead of a new row when I have situation like this from the exmaple:</p>
  
  <pre><code>new  {Row = 1, Col = 1, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
                  new  {Row = 1, Col = 2, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
  </code></pre>
  
  <p>Maybe I need a totally different approach, right now I think of some way using <code>foreach</code> and some nested logic there but can't think anything that's working right now.</p>
  
  <p>P.S</p>
  
  <p>Right now all I do in the view is :</p>
  
  <pre><code>@{
                foreach (var item in ViewBag.Header)
                {
  
                    &lt;tr&gt;&lt;td&gt;item.QuestionText&lt;/td&gt;&lt;td&gt;item.FieldValue&lt;/td&gt;&lt;/tr&gt;
  
                }
             }
  </code></pre>