title: How can I get the selected value from dropdownlist in edit mode of a gridview?
body: <p>In my application, when I edit a row in the gridview I choose some new data from a dropdownlist.</p>
  
  <p>I am populating the dropdown like this:</p>
  
  <pre><code> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              if ((e.Row.RowState &amp; DataControlRowState.Edit) &gt; 0)
              {
                  DropDownList emailsListDL = (DropDownList)e.Row.FindControl("emailsDL");
                  emailsListDL.DataSource = allUsersEmails;//list of strings with the emails of the users
                  emailsListDL.DataBind();
              }
          }
      }
  </code></pre>
  
  <p>But when I press the 'Update' button from the template and enters in the 'RowUpdating' event, the selected value from the dropdownlist is every time the first value from that dropdownlist.</p>
  
  <pre><code>protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          DropDownList emailsListDL = (DropDownList)GridViewAdvertisers.Rows[e.RowIndex].FindControl("emailsDL");
          string email = emailsListDL.SelectedValue; //the selected value is every time the first value from the dropdownlist
      }
  </code></pre>
  
  <p>Does anyone have any ideas?</p>
  
  <p>I've tried many ways to set the selected value in the 'RowDataBound' event, but with no luck. I tried this:</p>
  
  <pre><code>1. emailsListDL.SelectedIndex = emailsListDL.Items.IndexOf(emailsListDL.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "OwnerMail").ToString()));
  2. emailsListDL.SelectedValue = GridViewAdvertisers.DataKeys[e.Row.RowIndex]["OwnerMail"].ToString();
  3. emailsListDL.SelectedValue = GridViewAdvertisers.Rows[e.Row.RowIndex].Cells[1].Text;
  //ownerMail is a string (object) from the list of objects that I put as datasource to the gridview
  </code></pre>
  
  <p>Thanks,
  Jeff</p>
  
  <p><strong>Update</strong></p>
  
  <p>My Item template from the aspx page is:</p>
  
  <pre><code> &lt;asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
                  ItemStyle-Width="150px" HeaderText="Owner Email" HeaderStyle-HorizontalAlign="Left"
                  HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="#e1e1e1"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="LabelEmail" runat="server" Text='&lt;%# Bind("OwnerMail")%&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="emailsDL" runat="server" Width="150"&gt;
                      &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True"&gt;&lt;/HeaderStyle&gt;
                  &lt;ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="180px" BorderWidth="1px"
                      BorderColor="#e1e1e1"&gt;&lt;/ItemStyle&gt;
      &lt;/asp:TemplateField&gt;
  </code></pre>