title: How to loop through GridView rows which have EditItemTemplates
body: <p>I have a function which loops through all the rows of my GridView and compares some of the cell values with a value the user wants to insert into the table. The point is to make sure the value they want to insert is not a duplicate value.</p>
  
  <p>This function was working fine until I recently made most of the columns into EditItemTemplate controls so that I could access them on update. </p>
  
  <p>Now, depending on how I try to do it, I either get a blank string or I get a "Object reference not set to an instance of an object" error.</p>
  
  <p>Here's how I was originally finding the specific cell (which now apparently does nothing):</p>
  
  <pre><code>GridView1.Rows[rowIndex].Cells[2].Text
  </code></pre>
  
  <p>And here's how I'm doing it now (but which gives me the reference error):</p>
  
  <pre><code>GridView1.FindControl("storeNumberTB").ToString()
  </code></pre>
  
  <p>I suspect the issue is that I'm referencing a TextBox control within EditItemTemplate rather than just the label(?) which is what I presumably need to look at as I loop through the rows. But I can't figure out how to access the value.</p>
  
  <p>So I just need to get whatever text is at a particular cell on every row as it loops through. </p>
  
  <p>Here's the entire function as it is now in case that helps:</p>
  
  <pre><code>foreach (GridViewRow row in GridView1.Rows)
          {
            if (GridView1.FindControl("storeNumberTB").ToString() == userInputValue)
            {
              duplicate++;
            }
          }
  </code></pre>
  
  <p>Here's the relevant part of my ASPX code for the GridView:</p>
  
  <pre><code> &lt;asp:TemplateField HeaderText="Store"&gt;
                  &lt;ItemTemplate &gt;
                      &lt;asp:Label runat="server" Text='&lt;%# Eval("Store") %&gt;' &gt;
                      &lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate &gt;
                      &lt;asp:TextBox ID="storeNumberTB" runat="server" Text='
                      &lt;%# Eval("Store")%&gt;'  &gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
             &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong> row.Cells[3].Text returns an empty string. No clue why. Any idea how to access the value in the cell in my case?</p>