title: Binding a DataTable to Template Fields of a GridView
body: <p>I can successfully bind a DataTable to a GridView by auto generating columns, but I need to display a multi-lined cell for one of the columns. To do this, I want to use a template field with an item template using a TextBox object. I fill in the DataTable by adding columns and then adding the rows. I know my datatable is set up right because it shows all the data (except for the multi-lined cell) as I want it. My issue is getting the gridview to extract my data based on the column names and filling in the template fields I have set up. If  I turn AutoGenerateColumns off then the 4 templatefield columns still appear (in the correct amount according to the datatable too), just blank, and if I have it set to true, then the 4 blank columns appear as well as 4 additional columns with the same headers that contain my data using the defaults for what the cell contains to display the information.</p>
  
  <pre><code>    &lt;asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="False"     HeaderStyle-BorderStyle="None" CellPadding="3" ItemStyle-Wrap="true" &gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Code"&gt;
              &lt;asp:ItemTemplate&gt;
                  &lt;asp:Label ID="txtCode" runat="server" Text='&lt;%# Eval("Code") %&gt;'&gt;
  
                  &lt;/asp:Label&gt;
              &lt;/asp:ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Title"&gt;
              &lt;asp:ItemTemplate&gt;
                  &lt;asp:Label ID="txtTitle" runat="server" Text='&lt;%# Eval("Title") %&gt;'&gt;
  
                  &lt;/asp:Label&gt;
              &lt;/asp:ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Class"&gt;
              &lt;asp:ItemTemplate&gt;
                  &lt;asp:Label ID="txtClass" runat="server" Text='&lt;%# Eval("Class") %&gt;'&gt;
  
                  &lt;/asp:Label&gt;
              &lt;/asp:ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="History"&gt;
              &lt;asp:ItemTemplate&gt;
                  &lt;asp:TextBox ID="txtHistory" runat="server" IsReadOnly="true" Text='&lt;%# Eval("History")%&gt;'&gt;
                  &lt;/asp:TextBox&gt;
              &lt;/asp:ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The above is the portion of my asp.net code that relates to the GridView in question. Following is how i set up my DataTable and bind it.</p>
  
  <pre><code>DataTable table = new DataTable();
  table.Columns.Add("Code", typeof(string));
  table.Columns.Add("Title", typeof(string));
  table.Columns.Add("Class", typeof(string));
  table.Columns.Add("History", typeof(string));
  for (int i = 0; i &lt; index; i++)
  {
      table.Rows.Add(docs[i].Code, docs[i].Name, docs[i].Class.Name, history[i]);
  }
  DataGrid1.DataSource = table;
  DataGrid1.DataBind();
  </code></pre>