title: Binding a gridview via a dynamic DataTable
body: <p>I have a <code>GridView</code> and I need to bind it with a dynamic Data Table.</p>
  
  <p>I have a <code>List&lt;List&lt;string&gt;&gt;</code> and a <code>List&lt;List&lt;DateTime&gt;&gt;</code>.</p>
  
  <p>I need a DataTable with 3 columns, namely <code>FileName</code>, <code>LastAccessTimeForServer1</code>, <code>LastAccessTimeForServer2</code>.</p>
  
  <p>The <code>List&lt;List&lt;string&gt;&gt;</code> would contain 2 lists of filenames(common for both the servers), while <code>List&lt;List&lt;DateTime&gt;&gt;</code> would contain 2 lists of last access times (different for both the files).</p>
  
  <p>Below is the code which I have tried, which doesn't make much sense to me. </p>
  
  <p>Completely perplexed.</p>
  
  <pre><code>string serviceName = DropDownList1.SelectedItem.Text;
  List&lt;string&gt; serviceNameArray = new List&lt;string&gt;();
  for (int x = 1; x &lt;= 2; x++)
  {
  if (x &lt; 2)
  {
  serviceNameArray.Add(@"\\Server10" + x + @"\WebSite" + "\\" + serviceName);
  }
  else
  {
  serviceNameArray.Add(@"\\Server1" + x + @"\WebSite" + "\\" + serviceName);
  }
  }
  List&lt;List&lt;string&gt;&gt; ultimateList = new List&lt;List&lt;string&gt;&gt;();
  List&lt;int&gt; numList = new List&lt;int&gt;();
  //Call the Search method
  
  for (int y = 0; y &lt; 2; y++)
  {
  ultimateList.Add(Search(serviceNameArray[y]));
  }
  for (int z = 0; z &lt; 2; z++)
  {
  numList.Add(ultimateList[z].Count);
  }
  
  //FInd the Last Write Time for all the files.
  for (int xx = 0; xx &lt; 2; xx++)
  {
  listOfDateTimes.Add(new List&lt;DateTime&gt;());
  for (int yy = 0; yy &lt; numList[xx]; yy++)
  {
  listOfDateTimes[xx].Add(File.GetLastWriteTime(ultimateList[xx][yy]));
  }
  }
      DataTable dtForGridView = new DataTable();
      DataColumn dc1 = new DataColumn("File Name", typeof(string));
      DataColumn dc2 = new DataColumn("LastAccessTimeForServer1", typeof(DateTime));
      DataColumn dc3 = new DataColumn("LastAccessTimeForServer2", typeof(DateTime));
      dtForGridView.Columns.Add(dc1);
      dtForGridView.Columns.Add(dc2);
      dtForGridView.Columns.Add(dc3);
  
      for (int k = 0; k &lt;=2; k++)
      {
      for (int d = 0; d &lt; numList[k]; d++)//numList[k] will give the number of rows in each column
      {
      dtForGridView.Rows.Add(ultimateList[d][k], listOfDateTimes[k]);
      }
      }
      //Binding the GridView
      GridView1.DataSource = dtForGridView;
      GridView1.DataBind();
  </code></pre>
  
  <p>The final grid should look like:</p>
  
  <pre><code>FileName    LastAccessTimeForServer1          LastAccessTimeForServer1
  a.txt        Time1                             Time2
  b.txt        Time3                             Time4
  </code></pre>
  
  <p>Experts please help in finding a solution. 
  Any pointers will be highly appreciated.</p>
  
  <p>Regards</p>
  
  <p>Anurag</p>