title: Dynamic data table creation from a dymanic data source
body: <p>I need to create a datatable on the fly from a another datable such that I need to iterate through all the rows of Table A, do calculations on the column values and write the results into the table B. For example table A has 5 columns (but in reality the number of columns varies) starting with the second column (A), each column has a numeric value. I need to add them up and write the total into table B.</p>
  
  <p>Witht this the munber of clumns in table B will change in compared to table A but not rows. This data is not going to be displayed on the page as in a gridview or similar form so I can't use an OnEvent method, so please don't suggest other alternate ways, because the next process it requires a datatable to produce the desired outcome.</p>
  
  <pre><code>            | A | B | C   | D | E |        (table A)
      -------------------------------
      Entries | 2 | 6 | 100 | 0 | 5 |
  
  
              | Totals |                      (table B)
      Entries | 113    |
  </code></pre>
  
  <p>I've seen several examples suggesting progamatic data creation with this type of code:</p>
  
  <pre><code>DataTable dt; 
  protected void Page_Load(object sender, EventArgs e) 
  { 
  
  
      dt = new DataTable("tblTest"); 
      DataColumn dc1 = new DataColumn(); 
      dc1.DataType = typeof(String); 
      dc1.ColumnName = "Name"; 
      DataColumn dc2 = new DataColumn(); 
      dc2.DataType = typeof(String); 
      dc2.ColumnName = "Add1"; 
      DataColumn dc3 = new DataColumn(); 
      dc3.DataType = typeof(String); 
      dc3.ColumnName = "Add2"; 
  
      dt.Columns.Add(dc1); 
      dt.Columns.Add(dc2); 
      dt.Columns.Add(dc3); 
  
  } 
  
  
  
  protected void Button1_Click(object sender, EventArgs e) 
  { 
  
      Session["Name"] += txtName.Text + "|"; 
      Session["Add1"] += txtAdd1.Text + "|"; 
      Session["Add2"] += txtAdd2.Text + "|"; 
      CreateTable(); 
  
  } 
  public void CreateTable() { 
  
      string[] sa = Session["Name"].ToString().Split('|'); 
      string[] sb = Session["Add1"].ToString().Split('|'); 
      string[] sc = Session["Add2"].ToString().Split('|'); 
      int recordnum = sa.Length; 
  
      for (int j = 0; j &lt; recordnum - 1; j++) { 
  
          DataRow dr = dt.NewRow(); 
          dr["Name"] = sa[j].ToString(); 
          dr["Add1"] = sb[j].ToString(); 
          dr["Add2"] = sc[j].ToString(); 
          dt.Rows.Add(dr); 
  
      } 
  
      GridView1.DataSource = dt.DefaultView; 
      GridView1.DataBind(); 
  
  } 
  </code></pre>
  
  <p>but this not dynamic and I just can't wrap my hear around this. So I've started with a foreach loop to iterate through rows and cells of table A but not sure how to go about populating individual rows and cells in table B.</p>
  
  <pre><code>  foreach (DataRow row in _dt.Rows)
      {
          for (int i = 2; i &lt; row.ItemArray.Length; i++ )
          {
              if (row.ItemArray[i] != DBNull.Value)
                  _total += Convert.ToInt32(row.ItemArray[i]);
          }
      }
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>Risho</p>
  
  <p>Latest Update: As I was writing this post tiny little lighbulbs were turning on in my head and I produced this piece of code. It does not error out but it doesn't work either. For just test purposes I tried to bind to a gridview to see what data I'm getting - but no data. The statis column names are visible in the debugger and I know the source data table has good data. Here the code for yor inspection:</p>
  
  <pre><code>DataTable _newDt = new DataTable();
  
      DataColumn _dc1 = new DataColumn();
      _dc1.DataType = typeof(String);
      _dc1.ColumnName = "Wuc";
  
      DataColumn _dc2 = new DataColumn();
      _dc2.DataType = typeof(String);
      _dc2.ColumnName = "Wuc Entries";
  
      DataColumn _dc3 = new DataColumn();
      _dc2.DataType = typeof(String);
      _dc3.ColumnName = "Total Entries";
  
      _newDt.Columns.Add(_dc1);
      _newDt.Columns.Add(_dc2);
  
      int _total = 0;
  
      foreach (DataRow row in _dt.Rows)
      {
          DataRow _dr = _newDt.NewRow();
  
          DataColumn col1 = new DataColumn();
          col1.DataType = typeof(string);
          col1.ColumnName = row.ItemArray[0].ToString();
  
          DataColumn col2 = new DataColumn();
          col2.DataType = typeof(string);
          col2.ColumnName = row.ItemArray[1].ToString();
  
          DataColumn col3 = new DataColumn();
          col3.DataType = typeof(int);
  
          for (int i = 2; i &lt; row.ItemArray.Length; i++ )
          {
              if (row.ItemArray[i] != DBNull.Value)
                  _total += Convert.ToInt32(row.ItemArray[i]);
          }
  
          col3.ColumnName = _total.ToString();
      }
  </code></pre>
  
  <p>UPDATE #2:</p>
  
  <p>I'v made following changes and but a new error is popped up: </p>
  
  <blockquote>
    <p>"Input string was not in a correct format.Couldn't store  in Wuc Entries Column.  Expected type is Int32."</p>
  </blockquote>
  
  <p>The issue is that I've delcared the column as String, the data from parent table is in a string format, and so I don't understand why it expects an Int32. (?) Here is the new version:</p>
  
  <pre><code>    DataTable _newDt = new DataTable();
      DataRow _dr;
  
      DataColumn _dc1 = new DataColumn();
      _dc1.DataType = typeof(String);
      _dc1.ColumnName = "Wuc";
  
      DataColumn _dc2 = new DataColumn();
      _dc2.DataType = typeof(String);
      _dc2.ColumnName = "Wuc Entries";
  
      DataColumn _dc3 = new DataColumn();
      _dc2.DataType = typeof(Int32);
      _dc3.ColumnName = "Total Entries";
  
      _newDt.Columns.Add(_dc1);
      _newDt.Columns.Add(_dc2);
  
      int _total = 0;
  
      foreach (DataRow row in _dt.Rows)
      {
          _dr = _newDt.NewRow();
  
          _dr["Wuc"] = row.ItemArray[0].ToString();
          _dr["Wuc Entries"] = row.ItemArray[1].ToString();
  
          for (int i = 2; i &lt; row.ItemArray.Length; i++ )
          {
              if (row.ItemArray[i] != DBNull.Value)
                  _total += Convert.ToInt32(row.ItemArray[i]);
          }
  
          _dr["Total Entries"] = _total;
          _newDt.Rows.Add(_dr);
      }
  </code></pre>