views:

769

answers:

3

I have two tables in a database and using entity framework and ria services to display data in a simple datagrid. Here is the database/EF structure

Cars      Make
-----     ----
CarId     MakeId
Car       Make
MakeId

In my Silverlight datagrid I want to show the following two columns, for example

Car      Make
---      -----
Escort   Ford
Megane   Renault
Rav4     Toyota

I can easily bind to the Cars table and show cars but I can't work out how to display the Make taken from the child table

The xaml that I am using to configure the datagrid is as follows:

<datagrid:DataGrid x:Name="CarGrid" AutoGenerateColumns="False" MinHeight="100" IsReadOnly="True" ItemsSource="{Binding ElementName=MyData, Path=Data}">
                    <datagrid:DataGrid.Columns>

                        <datagrid:DataGridTextColumn Header="Car" Binding="{Binding Car}"/>
                        <datagrid:DataGridTextColumn Header="Make" Binding="{Binding Cars.Make}"/>
......

The datagrid datasource binds to a DomainDataSource method "GetCars". I'm not sure if it is automatically loading the child table (not sure whether I have to explicitly tell it to or not, and have no idea how to do this in xaml).

I'm sure I could ditch the xaml and do it in c# but I'm trying to be a good coder and do it in xaml.

A: 

Unless you specify it explicitly somewhere (using Linq-to-SQL's DataContext.LoadOptions for example), the child record wouldn't be loaded automatically with the parent record.

Here's how it's done in Linq-to-SQL:

List<tblInventory> result;
using (var dc = new SMDataContext())
{
    dc.Log = Console.Out;
    var dlo = new DataLoadOptions();
    dlo.LoadWith<tblInventory>(x => x.tblItemInfo);
    dc.LoadOptions = dlo;
    var q = (from o in dc.tblInventories
             select o).Take(10);
    result = q.ToList();
}

And here's the resulting log:

SELECT TOP 10 [t0].[fldItemPrivateSN], [t0].[fldCatNo], [t0].[fldSerNo], ...
              ..., [t1].[fldWeight], [t1].[fldValue], ...
FROM [dbo].[tblInventory] AS [t0]
INNER JOIN [dbo].[tblItemInfo] AS [t1] ON [t1].[fldCatNo] = [t0].[fldCatNo]
-- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 3.5.30729.1

Update: Also, it appears that you are binding to Cars.Make, try binding to Car.Make instead.

Aviad P.
I know about LoadOptions, but I am trying to do it in xaml rather than c#
Calanus
XAML doesn't know about SQL or queries or whatnot, it binds to a property. If the property's get accessor returns null, you won't see anything. There's no magic here.
Aviad P.
"xaml binds to a property" - exactly! And I am trying to bind to the Make property of the Cars object, in a similar way that I am binding to the Car property within the Cars table. Please also bear in mind that I am using RIA services and not accessing the EF directly.
Calanus
Have you tried binding to `Car.Make` instead of `Cars.Make`?
Aviad P.
A: 

Mike Hilberg's article addresses much of what you are interested in:

http://blogs.msdn.com/mikehillberg/archive/2009/03/26/implementing-selectedvalue-with-the-silverlight-combobox.aspx

as does Manish Dalal's article:

http://weblogs.asp.net/manishdalal/archive/2008/09/28/combobox-in-datagrid.aspx

Even with the help of these examples I struggled to implement the same sort of thing as you (using hand-crafted MVVM rather than RIA).

I ended up with an inelegant hack which did compromise the underlying objects (something you would like to avoid) and that was difficult enough.

Good luck.

mikemay
+1  A: 

You need to change your binding for the Make column to Car.Make, and you need to ensure this Property is being populated by EF.

I'd suggest attaching a debugger to the service and inspecting what is coming out of EF.

You can also attach SQL Profiler to your SQL Server instance and see what query EF is running.

Bryan Batchelder