title: Gridview row.Cells[i].Findcontrol finding control in wrong Cell
body: <p>I have a function I've written that you pass in the gridview row, as well as what control type you're attempting to locate. It finds the control as intended, however, it finds it in the incorrect cell.</p>
  
  <pre><code>protected int GetCell(GridViewRow row, string strSection)
  {
      int intVal = 0;
      for (int i = 0; i &lt; row.Cells.Count; i++)
      {
          intVal = i;
          if (strSection == "QueryRate")
          {
              DropDownList cmbQueryType = row.Cells[i].FindControl("cmbQueryType") as DropDownList;
              if (cmbQueryType != null)
              {
                  break;
              }
          }
          else if (strSection == "Name")
          {
              TextBox txtName = row.Cells[i].FindControl("txtName") as TextBox;
              if (txtName != null)
              {
                  break;
              }
          }
      }
      return intVal;
  }
  </code></pre>
  
  <p>From my understanding, and directly from the Microsoft page: Searches the <strong>current</strong> naming container for a server control with the specified id parameter.</p>
  
  <p>So my question is, why is this seemingly finding the control is the wrong column? I can't hard code specific columns, because the columns that show vary based on the the datasource.</p>
  
  <p>Per request, the gridview:</p>
  
  <pre><code>&lt;TDWeb:PageGridView SkinID="pagegrid" EmptyDataText="No unmatched parameters."  ID="gvUnmatched"  runat="server"  AllowSorting="True" 
                              OnSorting="gvUnmatched_Sorting" AutoGenerateColumns="False"  OnRowDataBound="gvUnmatched_RowDatabound"  OnDataBound="gvUnmatched_DataBound"
                              OnPreRender="gvUnmatched_PreRender" OnPageIndexChanging="OnFilterUnmatched" DataKeyNames="InternalName"
                              ShowFooter="true" OnPageSizeChanged="gvUnmatched_PageSizeChanged"&gt;
                      &lt;Columns&gt;
                          &lt;asp:TemplateField runat="server" ItemStyle-CssClass="c" FooterStyle-CssClass="c"&gt;
                              &lt;HeaderTemplate&gt;
                                  &lt;asp:Label runat="server" Text="Add" /&gt;
                                  &lt;asp:CheckBox runat="server" ID="ckbAddAll" /&gt;
                              &lt;/HeaderTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:CheckBox runat="server" ID="ckbAdd" EnableViewState="true" /&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" ItemStyle-CssClass="c"&gt;
                              &lt;HeaderTemplate&gt;
                                  &lt;asp:Label runat="server" Text="Ignore" /&gt;
                                  &lt;asp:CheckBox runat="server" ID="ckbIgnoreAll" /&gt;
                             &lt;/HeaderTemplate&gt;
                             &lt;ItemTemplate&gt;
                                 &lt;asp:CheckBox runat="server" ID="ckbIgnore" /&gt;
                             &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" HeaderText="Name" SortExpression="Name" ItemStyle-CssClass="l"&gt;
                              &lt;HeaderTemplate&gt;
                                  &lt;asp:LinkButton runat="server" Text="Name" CommandName="Sort" CommandArgument="Name" /&gt;
                                  &lt;td:HelperBox ID="HelperBox5" runat="server" ToolTipText="Set the Name for this parameter. This value must be unique to the CAN Bus or ECU and is the value used throughout the DAP system to reference this parameter." /&gt;
                              &lt;/HeaderTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:TextBox runat="server" ID="txtName" Width="250" MaxLength="256" style="background-color:inherit" Text='&lt;%# Bind("Name") %&gt;' /&gt; 
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" ItemStyle-CssClass="l" SortExpression="Description"&gt;
                              &lt;HeaderTemplate&gt;
                                  &lt;asp:LinkButton runat="server" Text="Description" CommandName="Sort" CommandArgument="Description" /&gt;
                                  &lt;td:HelperBox ID="HelperBox4" runat="server" ToolTipText="Set the Description for this parameter. This value is for informational purposes only. Not required." /&gt;
                              &lt;/HeaderTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:TextBox runat="server" ID="txtDescription" Width="175" MaxLength="500" 
                                      TextMode="multiline" style="background-color:inherit" Text='&lt;%# Bind("Description") %&gt;' /&gt; 
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" HeaderText="Length (bits)" SortExpression="LengthBits" ItemStyle-CssClass="r"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label runat="server" ID="txtSizeBits" Text='&lt;%# (int)Eval("LengthBits") + (int)Eval("LengthBytes") * 8 %&gt;' style="background-color:inherit" /&gt; 
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" AccessibleHeaderText="SourceAddress" HeaderText="SA" SortExpression="Sources" ItemStyle-CssClass="r"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label runat="server" ID="txtSources" Text='&lt;%# Eval("SourceString") %&gt;' /&gt; 
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" AccessibleHeaderText="ID" HeaderText="ID/Address" SortExpression="IDorAddress" ItemStyle-CssClass="r"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label runat="server" ID="txtIDorAddress" Text='&lt;%# Eval("IDorAddress") %&gt;' /&gt; 
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" HeaderText="Data Type" SortExpression="Datatype" ItemStyle-CssClass="l"&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label runat="server" ID="txtDatatype" Text='&lt;%# Bind("Datatype") %&gt;' /&gt; 
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" AccessibleHeaderText="QueryRate" ItemStyle-CssClass="r" SortExpression="QueryRate" HeaderText="Query Rate"&gt;
                              &lt;HeaderTemplate&gt;
                                  &lt;asp:Label runat="server" Text="Rate" /&gt;
                                  &lt;td:HelperBox ID="HelperBox9" runat="server" ToolTipText="Set the Query Type and Rate for this parameter (if applicable). If set, this value overrides the default Query set below." /&gt;
                              &lt;/HeaderTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:Label runat="server" ID="lblCANRate" Text='&lt;%# Eval("QueryRateString", "{0} s") %&gt;' Visible='&lt;%# m_SelectedFile.ImportType == App_Code.DAPConfig.ParameterFileImport.EImportType.CANDBC || (m_SelectedFile.ImportType == App_Code.DAPConfig.ParameterFileImport.EImportType.J1939DBC &amp;&amp; ((int)Eval("QueryRate")) &gt; 0) %&gt;' /&gt;
                                  &lt;asp:DropDownList runat="server" ID="cmbQueryType" style="background-color:inherit" Visible='&lt;%# m_SelectedFile.ImportType != App_Code.DAPConfig.ParameterFileImport.EImportType.CANDBC &amp;&amp; (m_SelectedFile.ImportType != App_Code.DAPConfig.ParameterFileImport.EImportType.J1939DBC || (int)Eval("QueryRate") &lt;= 0) %&gt;' /&gt;
                                  &lt;asp:Panel runat="server" id="pnlRate" style="display:none" &gt;&lt;asp:TextBox runat="server" ID="txtRate" Width="50" MaxLength="6" style="background-color:inherit" /&gt;s&lt;/asp:Panel&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:TemplateField runat="server" HeaderText="Category" ItemStyle-CssClass="l"&gt;
                              &lt;HeaderTemplate&gt;
                                  &lt;asp:Label runat="server" Text="Category" /&gt;
                                  &lt;td:HelperBox ID="HelperBox10" runat="server" ToolTipText="Set the Category with which to group this parameter, or create a new category." /&gt;
                              &lt;/HeaderTemplate&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:DropDownList runat="server" ID="cmbCategory" style="background-color:inherit" &gt;
                                      &lt;asp:ListItem Value="_New_" Text="Add New..." /&gt;
                                  &lt;/asp:DropDownList&gt;
                                  &lt;br /&gt;
                                  &lt;asp:TextBox Width="100" MaxLength="50" runat="server" ID="txtCategory" style="display:none;background-color:inherit" /&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                     &lt;/Columns&gt;
                 &lt;/TDWeb:PageGridView&gt;
  </code></pre>