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

A field or property with the name 'OrderID' was not found on the selected data source

在ASP.NET中处理多级网格视图,不断地得到这个错误。绑定列表导致此错误。GridViewOrder未获取orderID属性,但我已对其进行了定义。

课程代码为:

1
2
3
4
 public class Customer
    {
        public List<Order> Orders { get; set; }
    }

调用内部网格的函数是:

1
2
3
4
5
6
7
8
9
10
11
12
13
 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代码如下:

2


您需要在这里输入SelectMany,因为目前您正试图将GridView绑定到订单列表:

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