I have a DataGrid that looks like this (slightly simplified here):
<asp:DataGrid ID='grdQuotas' runat='server' AutoGenerateColumns='False'> <HeaderStyle CssClass='quotas-header' /> <Columns> <asp:TemplateColumn> <HeaderTemplate> Max order level</HeaderTemplate> <ItemTemplate> <asp:DropDownList ID='ddlMaxOrderLevel' runat='server' DataSourceID='xdsOrderLevel' DataTextField='Text' DataValueField='Value' SelectedValue='<%# Bind('MaxOrderLevel') %>'> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:XmlDataSource ID='xdsOrderLevel' runat='server' DataFile='~/App_Data/OrderLevels.xml'> </asp:XmlDataSource>
In my Page_Load event handler I am creating a DataTable containing default values and DataBinding it to the DataGrid.
The problem is that this is taking place before the DropDownList ddlMaxOrderLevel has been bound to its DataSource, so I get a runtime error telling me that the SelectedValue cannot be set.
If ddlMaxOrderLevel was not in a DataGrid I could just call DataBind() on it. However I cannot do that in this scenario – since it is in an ItemTemplate.
Can anyone suggest a workaround or alternate approach?
Create another DataSource and bind it to the DataGrid. Where the SelectMethod would return the default values in a simple object.
Then all the binding should happily work together.