title: Populating dynamic table in asp.net?
body: <p>I would really appreciate your help on this. This is my problem:</p>
  
  <p>I have a ArrayList of product objects. Each product has a id, name and a supplier.
  When I iterate the arraylist and creates a table to put  this value into cells I come to the problem that a product can have more than one supplier. If this is the case the id and name are the same but with a different supplier in the arraylist.
  My code so far takes care of this by creating empty cells for id and name and put the other supplier in a new cell. </p>
  
  <p>But, it doesn't look good to create new rows for every supplier. What I want is that if a product has more than one supplier I want all the suppliers in the same cell on the row for the product id.</p>
  
  <pre><code>string id = string.Empty;
  int count = 0;
  public void CreateResultTable(ArrayList result)
  {
      TableRow row;
      TableCell cell;
  
      if (result != null &amp;&amp; result.Count &gt; 0)
      {
          foreach (Item item in result)
          {
  
              if (count == 0)
              {
                  row = new TableRow();
                  id = item.id;
  
                  cell = new TableCell();
                  cell.Text = item.id;
                  row.Cells.Add(cell);
  
                  cell = new TableCell();
                  cell.Text = item.product;
                  row.Cells.Add(cell);
  
                  cell = new TableCell();
  
                  ArrayList levList = item.suppliers;
                  if (levList != null)
                  {
  
                      string lev = string.Empty;
                      for (int i = 0; i &lt; levList.Count; i++)
                      {
                          lev += levList[i];
                      }
                      cell.Text = lev;
                      row.Cells.Add(cell);
  
                  }
                  else
                      cell.Text = string.Empty;
                      row.Cells.Add(cell);
  
                  count++;
              }
              else if (id != item.id)
              {
  
                  row = new TableRow();
                  id = item.id;
  
                  cell = new TableCell();
                  cell.Text = item.id;
                  row.Cells.Add(cell);
  
                  cell = new TableCell();
                  cell.Text = item.product;
                  row.Cells.Add(cell);
  
                  cell = new TableCell();
  
                  ArrayList levList = item.suppliers;
                  if (levList != null)
                  {
  
                      string lev = string.Empty;
                      for (int i = 0; i &lt; levList.Count; i++)
                      {
                          lev += levList[i];
                      }
                      cell.Text = lev;
  
                  }
                  else
                      cell.Text = string.Empty;
                  row.Cells.Add(cell);
  
  
              }
              else
              {
                  row = new TableRow();
                  cell = new TableCell();
                  cell.Text = string.Empty;
                  row.Cells.Add(cell);
  
                  cell = new TableCell();
                  cell.Text = string.Empty;
                  row.Cells.Add(cell);
  
                  cell = new TableCell();
                  ArrayList levList = item.suppliers;
                  if (levList != null)
                  {
                      string lev = string.Empty;
                      for (int i = 0; i &lt; levList.Count; i++)
                      {
                          lev += levList[i];
                      }
                      cell.Text = lev;
                      row.Cells.Add(cell);
                  }
                  else
                      cell.Text = string.Empty;
                  row.Cells.Add(cell);
              }
  
              SearchResultLev.Rows.Add(row);
  
  
  
          }
  
  
          SearchResultLev.Visible = true;
          SearchResult.Visible = false;
          NoSearchResult.Visible = false;
      }
  
      else
      {
          SearchResultLev.Visible = false;
          SearchResult.Visible = false;
          NoSearchResult.Visible = true;
      }
  
  }
  </code></pre>