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 开发者_JS百科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.
I find a good solution. Let's try this: http://jeffhandley.com/archive/2010/03/12/lookupdata.aspx
Then don't forget to "include" your child table in the Query lookup on your domain service class.
ex:
public IQueryable<Car> GetCars()
{
return this.ObjectContext.Cars.Include("Make");
}
replace the names with your own.
PS: remember to do all the steps that the blog indicates.
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.
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.
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.
精彩评论