在视图中以其值替换(外键的)ID值

时间:2012-03-23 18:18:26

标签: c# wpf linq-to-sql data-binding datacontext

我有两个表,由外键CarrierID绑定:

Carrier[CarrierID,CarrierName] 
CarrierID = 1, CarrierName = DHL
CarrierID = 2, CarrierName = Fedex

...

Vendor[VendorID, VendorName, CarrierID]  
VendorID = 1, VendorName =D-link , CarrierID=1  
VendorID = 2, VendorName = Netbes , CarrierID= 2

我如何在供应商gridView而不是CarrierID中显示其值(即:CarrierName)? 实施例“

View Vendor gridview
1 | D-link | DHL
2 | Netbes | Fedex

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以在数据库中创建一个视图来完成您想要的任务:

SELECT v.VendorName, c.CarrierName FROM Vendor v
  JOIN Carrier c ON c.CarrierId = v.CarrierId

答案 1 :(得分:0)

我认为你的意思是LinqToSql:

var context = new DataContext();
System.Data.Linq.DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Vendor>(v => v.Carrier);
context.LoadOptions = dlo;

...

gridview.DataContext/ItemsSource = /*my query result of Vendor*/ IQueryable<Vendor>.ToList();

在您的GridView中

{Binding Path=Carrier.CarrierName, FallBackValue=''}

设置FallbackValue是因为Carrier可以为null。