名称为'OrderID'在选定的数据源上找不到
本文关键字:数据源 找不到 OrderID | 更新日期: 2023-09-27 18:05:26
在asp.net的多层网格视图中工作,连续得到这个错误。绑定列表导致此错误。GridViewOrder没有获得OrderID属性,但我已经定义了它。
类的代码如下:
public class Customer
{
public List<Order> Orders { get; set; }
}
调用内网格的函数为:
protected void GridViewCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string customerID = GridViewCustomer.DataKeys[e.Row.RowIndex].Value.ToString();
GridView GridViewOrders = (GridView)e.Row.FindControl("GridViewOrders");
var lstOrders = from orders in SortCustomer()
where orders.CustomerID == customerID
select orders.Orders;
GridViewOrders.DataSource = lstOrders.ToList();
GridViewOrders.DataBind(); // This line generating error
}
}
aspx代码如下:
<asp:TemplateField HeaderText="Orders">
<ItemTemplate>
<asp:GridView ID="GridViewOrders" runat="server"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="Order Id" />
<asp:BoundField DataField="OrderDate" HeaderText="Order Date" />
<asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:c}" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
这里需要SelectMany
,因为当前您正在尝试将GridView
绑定到订单列表的列表:
var lstOrders = SortCustomer().Where(c => c.CustomerID == customerID)
.SelectMany(c => c.Orders);