在所选数据源上找不到名为“OrderID”的字段或属性

时间:2014-05-26 15:23:09

标签: c# asp.net linq gridview aspxgridview

在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>

1 个答案:

答案 0 :(得分:2)

您需要SelectMany,因为目前您正在尝试将GridView绑定到订单列表列表:

var lstOrders = SortCustomer().Where(c => c.CustomerID == customerID)
                              .SelectMany(c => c.Orders);