DataGridView datasource : Use BindingSource or direct query ???

LINQ to SQL translates LINQ queries to SQL for execution on a database. The results are strongly typed IEnumerable. Because these objects are ordinary common language runtime (CLR) objects, ordinary object data binding can be used to display the results. On the other hand, sorting and change operations (inserts, updates, and deletes) require additional steps.

For example, to provide a DataSource for a DataGridView that can be sorted using the DataGridView’s built in sorting capabilities, you can not assisign a LINQ TO SQL query directly to the DataGridView’s DataSource.

Instead, assign a LINQ TO SQL query to a BindingSource and then assign the BindingSource to a DataGridView.

public BindingSource OrderList(Int64 ClientID)
{
        BindingSource bs = new BindingSource();
        bs.DataSource = from o in DB.tblOrders
                        where o.ClientID == ClientID
                        select o;
        return bs;
}

Bind the BindingSource to a DataGridView:

private void LoadOrdersGridView()
{
        OrdersGridView.DataSource = OrderList(10);
}


Leave a Reply