title: Building a htmlTable with dynamic rows and columns using C#
body: <p>I have a bit of a dilemma. I have a dynamic number of items that I would like to convert into a htmlTable that will allow me to dynamically determine the number of columns and rows that I want the table to have.</p>
  
  <p>For example, say I have a list of 5 items. I would like to be able to have the table built and rendered dynamically as a 1x1, 2x1, 2x2, etc..</p>
  
  <p>This is what I was able to come up with so far, but I keep getting an out of range error which tells me I haven't got the logic down for creating the right number of cells before adding the list of items. I got stuck trying to figure out how to build the table while accounting for the custom columns, rows, and the dynamic list.</p>
  
  <pre><code>HtmlTable myTable = new HtmlTable();
  HtmlTableRow row;
  HtmlTableCell cell;
  
  int desiredNumberOfColumns = 1; //How many Columns I want
  int desiredNumberOfRows = 5; //How many Rows I want
  
  int customColumn = desiredNumberOfColumns / rivers.Count;
  int customRow = desiredNumerOfColumns % rivers.Count;
  
  List&lt;string&gt; rivers = new List&lt;string&gt;(new string[]
  {
      "nile",
      "amazon",     // River 2
      "yangtze",    // River 3
      "mississippi",
      "yellow"
  });
  
  
      for(int i=0; i&lt;(desiredNumberOfRows / rivers.Count); i++) //
      {
         row = new HtmlTableRow();
         for(int j=0;j&lt;(customRow / rivers.Count);++j)
         {
            row.Cells.Add(new HtmlTableCell(rivers[i]));
         }
         myTable.Rows.Add(row);
      }
  </code></pre>
  
  <p>This example should build a table with 5 rows, and 1 column that looks like the following:</p>
  
  <pre><code>___________
  Row |Column
   0  |nile
   0  |amazon
   0  |yangtze
   0  |mississippi
   0  |yellow
  -----------
  </code></pre>