views:

55

answers:

1

Hi, looking for a way to create Linq-to-SQL Master-Detail WinForms DataGridViews where the Detail part of the Linq query is a custom one. Can do fine this:

DataClasses1DataContext db = new DataClasses1DataContext(".\\SQLExpress");
var myQuery = from o in db.Orders select o;
dataGridView1.DataSource = new BindingSource()
{
    DataSource = myQuery
};
dataGridView2.DataSource = new BindingSource()
{
    DataSource = dataGridView1.DataSource,
    DataMember = "OrderDetails"
};

but I'd like to have the Detail part under my precise control, like

var myQuery = from o in db.Orders join od in db.OrderDetails  
on o.ID equals od.OrderID into MyOwnSubQuery select o;

and use it for the second grid:

dataGridView2.DataSource = new BindingSource()
{
  DataSource = dataGridView1.DataSource,
  DataMember = "MyOwnSubQuery" // not working...
};

the real reason I want it is a bit more complex (I'd like to have the Detail part to be some not-pre-defined join actually), but I'm hoping the above conveyed the idea.

Can I only have the Detail part as a plain sub-table coming out of the pre-defined relation or can I do more complex stuff with the Detail part? Does anyone else feel this is kind of limited (if the first example is the best we can do)? Thanks!

A: 

Shot in the dark:

public class MyDataSource
{
    DataClasses1DataContext db = new DataClasses1DataContext(".\\SQLExpress");

    public IQueryable<Order> AllOrders
    {
        get { return db.Orders; }
    }

    public IQueryable<OrderDetails> OrderDetails
    {
        get
        {
            // define your custom query here
        }
    }
}

MyDataSource dataSource = new MyDataSource();
dataGridView2.DataSource = new BindingSource()
{
    DataSource = dataSource.AllOrders;
    DataMember = dataSource.OrderDetails;
};
Jay
Thanks! But how hard did I try I couldn't get there. At the point of "my custom query" it has to depend on the master selection, but how to know that? And the DataMember is a string property really. IQueryable feels like the right direction, anyway.
Andres