title: Gridview row editing - dynamic binding to a DropDownList
body: <p>I'm trying to get an ASP.NET 3.5 GridView to show a selected value as string when being displayed, and to show a DropDownList to allow me to pick a value from a given list of options when being edited. Seems simple enough?</p>
  
  <p>My gridview looks like this (simplified):</p>
  
  <pre><code>&lt;asp:GridView ID="grvSecondaryLocations" runat="server" 
                DataKeyNames="ID" OnInit="grvSecondaryLocations_Init" 
                OnRowCommand="grvSecondaryLocations_RowCommand" 
                OnRowCancelingEdit="grvSecondaryLocations_RowCancelingEdit"
                OnRowDeleting="grvSecondaryLocations_RowDeleting"
                OnRowEditing="grvSecondaryLocations_RowEditing" 
                OnRowUpdating="grvSecondaryLocations_RowUpdating"  &gt;
  &lt;Columns&gt;
      &lt;asp:TemplateField&gt;
           &lt;ItemTemplate&gt;
                &lt;asp:Label ID="lblPbxTypeCaption" runat="server" 
                                   Text='&lt;%# Eval("PBXTypeCaptionValue") %&gt;' /&gt;
           &lt;/ItemTemplate&gt;
           &lt;EditItemTemplate&gt;
                        &lt;asp:DropDownList ID="ddlPBXTypeNS" runat="server" 
                                 Width="200px" 
                                 DataTextField="CaptionValue" 
                                 DataValueField="OID" /&gt;
           &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The grid gets displayed OK when not in editing mode - the selected PBX type shows its value in the asp:Label control. No surprise there.</p>
  
  <p>I load the list of values for the DropDownList into a local member called <code>_pbxTypes</code> in the <code>OnLoad</code> event of the form. I verified this - it works, the values are there.</p>
  
  <p>Now my challenge is: when the grid goes into editing mode for a particular row, I need to bind the list of PBX's stored in <code>_pbxTypes</code>. </p>
  
  <p>Simple enough, I thought - just grab the drop down list object in the <code>RowEditing</code> event and attach the list:</p>
  
  <pre><code>protected void grvSecondaryLocations_RowEditing(object sender, GridViewEditEventArgs e)
  {
      grvSecondaryLocations.EditIndex = e.NewEditIndex;
  
      GridViewRow editingRow = grvSecondaryLocations.Rows[e.NewEditIndex];
  
      DropDownList ddlPbx = (editingRow.FindControl("ddlPBXTypeNS") as DropDownList);
      if (ddlPbx != null)
      {
          ddlPbx.DataSource = _pbxTypes;
          ddlPbx.DataBind();
      }
  
      .... (more stuff)
  }
  </code></pre>
  
  <p>Trouble is - I never get anything back from the <code>FindControl</code> call - seems like the <code>ddlPBXTypeNS</code> doesn't exist (or can't be found).</p>
  
  <p>What am I missing?? Must be something really stupid.... but so far, all my Googling, reading up on GridView controls, and asking buddies hasn't helped.</p>
  
  <p>Who can spot the missing link? ;-)</p>