I have a DataGrid that looks like this (slightly simplified here):
我有一个看起来像这样的DataGrid(这里稍微简化):
<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 DataBind
ing it to the DataGrid
.
在我的Page_Load事件处理程序中,我正在创建一个包含默认值的DataTable,并将它绑定到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.
问题是这是在DropDownList ddlMaxOrderLevel绑定到其DataSource之前发生的,因此我收到运行时错误,告诉我无法设置SelectedValue。
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
.
如果ddlMaxOrderLevel不在DataGrid中,我可以在其上调用DataBind()。但是在这种情况下我不能这样做 - 因为它在ItemTemplate中。
Can anyone suggest a workaround or alternate approach?
任何人都可以提出解决方法或替代方法吗?
2 个解决方案
#1
0
Create another DataSource and bind it to the DataGrid. Where the SelectMethod would return the default values in a simple object.
创建另一个DataSource并将其绑定到DataGrid。 SelectMethod将返回简单对象中的默认值。
Then all the binding should happily work together.
然后所有绑定应该愉快地一起工作。
#2
1
You could do the Databinding of the DropDownlist in the Databound event of the DataGrid.
您可以在DataGrid的Databound事件中执行DropDownlist的数据绑定。
Edit: I will give you an example that i have tested:
编辑:我会给你一个我测试过的例子:
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
DropDownList dl = (DropDownList)((DataGridItem)e.Item).FindControl("ddlMaxOrderLevel");
dl.DataSource = levels;
dl.DataBind();
dl.SelectedValue = ((DataRowView)e.Item.DataItem)["number"].ToString();
}
}
#1
0
Create another DataSource and bind it to the DataGrid. Where the SelectMethod would return the default values in a simple object.
创建另一个DataSource并将其绑定到DataGrid。 SelectMethod将返回简单对象中的默认值。
Then all the binding should happily work together.
然后所有绑定应该愉快地一起工作。
#2
1
You could do the Databinding of the DropDownlist in the Databound event of the DataGrid.
您可以在DataGrid的Databound事件中执行DropDownlist的数据绑定。
Edit: I will give you an example that i have tested:
编辑:我会给你一个我测试过的例子:
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
DropDownList dl = (DropDownList)((DataGridItem)e.Item).FindControl("ddlMaxOrderLevel");
dl.DataSource = levels;
dl.DataBind();
dl.SelectedValue = ((DataRowView)e.Item.DataItem)["number"].ToString();
}
}