UIS
UIS

Reputation: 27

DataGridView is not able to populate by LINQ to SQL object

I'm trying to populate a DataGridView on my WinForms application (dgvOrderSizes) using the code below. As commented in code, if I assign cod to my DataGridView it populates fine but when I try to assign orderDetails to it, it doesn't get populated. I tried debugging the code and found that DataSource property is assigned properly, so I wonder why the grid doesn't get populated? (There are no Exceptions raised)

    protected void ShowOrderDetails(int orderNo)
    {

        this.dgvOrderSizes.DataSource = null;

        var cod = from orderDetail in db.OrderDetails
                                 where orderDetail.OrderNo == orderNo
                                 select orderDetail;

        //this.dgvOrderSizes.DataSource = cod;        //works until here


        var orderDetails = from pOrder in cod.AsEnumerable()
                                 select new { pOrder.Plies, pOrder.Composition, pOrder.Size, pOrder.UnitName, pOrder.Quantity, pOrder.Rate, pOrder.ProductionUnitName, pOrder.DeliveryUnitName };

        this.dgvOrderSizes.DataSource = orderDetails;       //doesn't work
    }

Upvotes: 1

Views: 603

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062770

DataGridView uses a quite astonishing number of APIs, including IListSource, IList, ITypedList, IBindingList, IBindingListView, and most of CompnentModel (including all the descriptor stuff), and a rage more. If you say it works with "cod" then presumably LINQ-to-SQL is providing at least one of those APIs, and LINQ-to-Objects is not (which is fine; itisn't required to).

Just call ToList() and all will work:

blah.DataSource = orderDetails.ToList();

Upvotes: 2

Related Questions