title: dropdownlist into an ASP.Net GridView not binding correctly
body: <p>I have a <code>GridView</code> with a <code>DropDownList</code>.
  The <code>DropDownList</code> items need to be set into the code behind.
  I checked online code samples and it looks I am supposed to code it this way:</p>
  
  <pre><code>&lt;asp:GridView ID="DG_Table" runat="server" style="z-index: 1;autogeneratecolumns="False" 
  onrowcommand="DG_Table_RowCommand"  
  onrowdatabound="onrowdatabound"&gt;  
  
     &lt;Columns&gt;
       &lt;asp:TemplateField HeaderText="Name" &gt;
        &lt;ItemTemplate&gt;
           &lt;asp:DropDownList ID="Name" runat="server" Text='&lt;%# Bind("Name") %&gt;'&gt;&lt;/asp:TextBox&gt;
        &lt;/ItemTemplate&gt;
     &lt;asp:TemplateField HeaderText="field1" &gt;
        &lt;ItemTemplate&gt;
          &lt;asp:TextBox ID="field1" runat="server" Text='&lt;%# Eval("FieldValue") %&gt;'&gt;&lt;/asp:TextBox&gt;
       &lt;/ItemTemplate&gt;
   &lt;/Columns&gt;
   &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Into code behind:</p>
  
  <pre><code>public void onrowdatabound(object sender, GridViewRowEventArgs e)
          {
  
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  //check if is in edit mode
                  if ((e.Row.RowState &amp; DataControlRowState.Edit) &gt; 0)
                  {
  
                      DropDownList dropdownlist = (DropDownList)e.Row.FindControl("Name");
                      dropdownlist.DataSource = new List&lt;string&gt;() { "Pete", "Jack", "Steve", "Mike", "Rob", "Jim", "Eric" };
                      dropdownlist.DataBind();
                  }
              }
          }
  </code></pre>
  
  <p>When debugging, I can see that the debugger enters onrowdatabound.
  At this time, 
  <code>e.Row.RowType == DataControlRowType.Header</code></p>
  
  <p>This is probably normal at this stage. So the debugger exits the method.
  I would expect the debugger to enter <code>onrowdatabound</code> again when attempting to add lines to the <code>DataGrid</code>.
  Somehow it does not, so the <code>Datasource</code> is never set and the code later on tries to add a value to the <code>dropdownlist</code> which does not belong to the <code>dropdownlist.Items</code>, causing an execution error:</p>
  
  <p>Additional information: <code>'Name' has a SelectedValue which is invalid because it does not exist in the list of items</code>.</p>
  
  <p>(which is logical since the <code>dropdownlist.datasource</code> has never been set.</p>
  
  <p>When <code>DG_Table.DataBind()</code> is called, there are 3 rows:</p>
  
  <pre><code>protected void B_Fill_Fennec_Click(object sender, EventArgs e)
          {
              List&lt;fennec&gt; list_fennec = new List&lt;fennec&gt;() { new fennec("Pete", "dev"), new fennec("Jack", "butcher"), new fennec("Steve", "wood chopper"), };
  
              GridView1.DataSource = list_fennec.Select(x =&gt; new { Name = x.Name, FieldValue = x.Occupation });
              GridView1.DataBind();
          }
  </code></pre>
  
  <p>Any help appreciated. </p>