title: Why can this method not find the column of this GridViewDataRow?
body: <p>I have a GridView that I am trying to convert to a DataTable. I cannot use the GridView's DataSource for this DataTable. I've found the method below to be the most common way most use to tackle this task, but for some reason, the below method keeps throwing an IndexOutOfRangeException of "Cannot find column 0". Here's the code:</p>
  
  <blockquote>
  <pre><code>    DataSet ds = new DataSet("Student");
      DataTable dt = new DataTable(String.Format("{0}For{1}", gv.ID.Substring(2, gv.ID.Length - 2), year));
      for (int j = 0; j &lt; gv.Rows.Count; j++)
      {
          DataRow dr;
          GridViewRow row = gv.Rows[j];
          dr = dt.NewRow();
          for (int i = 0; i &lt; row.Cells.Count; i++)
          {
              dr[i] = row.Cells[i].Text; //***EXCEPTION IS THROWN HERE***
          }
          dt.Rows.Add(dr);
      }
      ds.Tables.Add(dt);
  </code></pre>
  </blockquote>
  
  <p>There are 18 cells per row.</p>
  
  <p>Thanks in advance for any help.</p>
  
  <p><strong>Solution</strong>
  I took CptSupermrkt's advice and implemented this code just before the first "for" loop, and the method continued w/o throwing the exception:</p>
  
  <pre><code>    DataColumn dc;
      foreach (var item in gv.Columns)
      {
          dc = new DataColumn(item.ToString());
          dt.Columns.Add(dc);
      }
  </code></pre>