title: How to Bind SelectedValues of DataBase values to Dropdown list in a Grid?
body: <p>I have Grid with DropDownList and it has static data. It is being saved perfectly. But, while editing, I need to get <code>SelectedValue</code> of the DropDown from the DataBase. </p>
  
  <p>My code is:</p>
  
  <pre><code>&lt;asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Order"&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:DropDownList ID="ddlOrder" SelectedValue='&lt;%# (DataBinder.Eval(Container.DataItem,"Order")) %&gt;' runat="server" Width="60"&gt;
              &lt;asp:ListItem Text="1" Value="1"&gt;&lt;/asp:ListItem&gt;
               &lt;asp:ListItem Text="2" Value="2"&gt;&lt;/asp:ListItem&gt;
               &lt;asp:ListItem Text="3" Value="3"&gt;&lt;/asp:ListItem&gt;
               &lt;asp:ListItem Text="4" Value="4"&gt;&lt;/asp:ListItem&gt;
          &lt;/asp:DropDownList&gt;
  
      &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>protected void gvServicePort_RowDataBound(object sender , GridViewRowEventArgs e)
  {
      //DropDown
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          var dropdownList = (DropDownList)e.Row.FindControl("ddlOrder");
          for (int i = 1; i &lt; 15; i++)
          {
              dropdownList.Items.Add(i.ToString());
          }
          dropdownList.SelectedValue =
                Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Order"));
      }
  }
  </code></pre>
  
  <p>It is throwing and error that say that 'ddlOrder' has a SelectedValue which is invalid because it does not exist in the list of items.</p>
  
  <p>Can anyone help?</p>