title: DataBound DropDownList in DataGrid - order of binding
body: <p>I have a DataGrid that looks like this (slightly simplified here):</p>
  
  <pre><code>&lt;asp:DataGrid ID="grdQuotas" runat="server" AutoGenerateColumns="False"&gt;
      &lt;HeaderStyle CssClass="quotas-header" /&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateColumn&gt;
              &lt;HeaderTemplate&gt;
                  Max order level&lt;/HeaderTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:DropDownList ID="ddlMaxOrderLevel" runat="server" DataSourceID="xdsOrderLevel"
                      DataTextField="Text" DataValueField="Value" SelectedValue='&lt;%# Bind("MaxOrderLevel") %&gt;'&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateColumn&gt;
      &lt;/Columns&gt;
  &lt;/asp:DataGrid&gt;
  
  &lt;asp:XmlDataSource ID="xdsOrderLevel" runat="server" DataFile="~/App_Data/OrderLevels.xml"&gt;
  &lt;/asp:XmlDataSource&gt;
  </code></pre>
  
  <p>In my <code>Page_Load</code> event handler I am creating a <code>DataTable</code> containing default values and <code>DataBind</code>ing it to the <code>DataGrid</code>.</p>
  
  <p>The problem is that this is taking place <em>before</em> the <code>DropDownList</code> <strong>ddlMaxOrderLevel</strong> has been bound to its <code>DataSource</code>, so I get a runtime error telling me that the <code>SelectedValue</code> cannot be set.</p>
  
  <p>If <strong>ddlMaxOrderLevel</strong> was not in a <code>DataGrid</code> I could just call <code>DataBind()</code> on it. However I cannot do that in this scenario - since it is in an <code>ItemTemplate</code>.</p>
  
  <p>Can anyone suggest a workaround or alternate approach?</p>