title: How to dynamically make DataTables and with Columns and Rows being dynamically generated as well?
body: <p>Say I have a database with 2 tables with the following names (<code>tbl1</code> and <code>tbl2</code>).
  Each of the table above have different number of columns, <code>tbl1</code> has 3 while <code>tbl2</code> has 4 columns.  </p>
  
  <p>I need to copy each of the above tables in a <code>DataTable</code>.  </p>
  
  <p>Of course I can do it manually like the following codes below:  </p>
  
  <pre><code>    class 
      {  
        public Main()  
        {
          // for tbl1 with 3 columns.
          string sql = "select * from tbl1"
          MySqlCommand com = new MySqlCommand(sql, con);
          MySqlDataReader dr = com.ExecuteDataReader();
          DataTable dt = GetDataTable(3);
  
          while (dr.Read())
          {
            if (dr.HasRows)
            {
              dt.Rows.Add(dr[0], dr[1], dr[2]);  &lt;---- Point of interest
            }
          }
  
          // for tbl2 with 4 columns.
          string sql = "select * from tbl2";
          MySqlCommand com = new MySqlCommand(sql, con);
          MySqlDataReader dr = com.ExecuteDataReader();
          DataTable dt = GetDataTable(4);
  
          while (dr.Read())
          {
            if (dr.HasRows)
              {
                dt.Rows.Add(dr[0], dr[1], dr[2], dr[3]);  &lt;---- Point of interest
              }
          }
      }
  
        public DataTable GetDataTable(int columnCount)
        {
          DataTable dt = new DataTable();
  
          if (columnCount &gt; 0)
          {
            for (int i = 0; i &lt; length; i++)
            {
              dt.Columns.Add(i.ToString(), typeof(object));
            }
          }
          return dt;
        }
      }  
  </code></pre>
  
  <p>But what I would like to do is to make the process above in an automated way, especially on the part where I indicated an arrow.</p>
  
  <p>Is there a way I could dynamically add rows like what I did on the columns?</p>
  
  <p>I was thinking I could add rows dynamically by using a function that generates the process of adding rows as string and call that string as a command but I am really really lost and don't know what to do... pls see the code below.</p>
  
  <pre><code>EX:    
  String generated from a function base on number of columns:
      "dt.Rows.Add(dr[0], dr[1], dr[2], dr[3])"
  Then, use the string as a command to add rows...
  </code></pre>