title: Accessing GridView data from a templatefield
body: <pre><code>    &lt;asp:GridView ID="gvGrid" runat="server" AutoGenerateColumns="False" 
          DataSourceID="dsDataSource" AllowPaging="True" PageSize="20" &gt;
          &lt;Columns&gt;
              &lt;asp:BoundField DataField="Field1" HeaderText="Field1" 
                  SortExpression="Field1" /&gt;
              &lt;asp:BoundField DataField="Field2" HeaderText="Field2" 
                  SortExpression="Field2" /&gt;
              &lt;asp:TemplateField HeaderText="TemplateField1"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label id="lblComments" runat="server" Text=" i use a function to compute"&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt; 
              &lt;/asp:TemplateField&gt;
  
            &lt;asp:CommandField ShowSelectButton="True" SelectText="Complete" HeaderText ="Status" /&gt;
  
              &lt;asp:TemplateField HeaderText="Action"&gt;
                  &lt;ItemTemplate&gt;
                         &lt;asp:Button ID="btnComplete" runat="server" Text="Complete" onclick="btnComplete_Click"/&gt;
                      &lt;asp:Button ID="btnAddComment" runat="server" Text="Add Comment" onclick="btnAddComment_Click" /&gt;    
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>protected void btnComplete_Click(object sender, EventArgs e) 
  {
  
      String Field1 = gvGrid.SelectedRow.Cells[1].Text; // throws an error at runtime
  
     //I want to be able to access the row data do some computation and then be able to insert it into the database 
  // Reason why I am trying to use it as a template field instead of a commandfield is because I want to make it not visible when it meets certain condition. 
  }
  </code></pre>
  
  <p>Also, it would be great if you could let me know a better way to do it, maybe using the command field and if there is a way to toggle its visibility. I don't mind using LinkButton either. </p>