多表联合查询结果绑定到DataGrid,为什么没有数据显示!
- C# code
var result = from ct in this.dmdc.ctTable join cts in this.dmdc.ctsTable on ct.id equals cts.checkTemplateId join ci in this.dmdc.ciTable on cts.checkItemId equals ci.id join it in this.dmdc.itTable on ci.typeId equals it.id select new { CheckTemplateId = ct.id, TypeId = ci.typeId, TypeName = it.typeName, ItemName = ci.itemName, StandardScore = ci.standardScore, StandardContent = ci.standardContent, Requirement = ci.requirement, } ; this.dataGrid1.ItemsSource = result;像上面的多表联合查询,跟踪到最后一行,发现result.Count() = 6。但是dataGrid1却不显示数据。请教高手。
如果设置成自动生成列,则可以把列名绑定到上面,但是就是不显示数据。
[解决办法]
不知道你写的是简化代码,还是实际代码?你的dmdc.ctTable、cts、ci、it实体都有值吗?
[解决办法]
另外,你的dataGrid1列是怎样绑定的,是自动绑定还是指定下面这些列:
CheckTemplateId = ct.id,
TypeId = ci.typeId,
TypeName = it.typeName,
ItemName = ci.itemName,
StandardScore = ci.standardScore,
StandardContent = ci.standardContent,
Requirement = ci.requirement,
[解决办法]
是SL还是WPF,如果是SL,应该是不行的,SL中好像不支持绑定匿名类,你最好定义个新类,然后在Linq中返回指定类型的对象集合,比如:
- C# code
var result = from ct in this.dmdc.ctTable join cts in this.dmdc.ctsTable on ct.id equals cts.checkTemplateId join ci in this.dmdc.ciTable on cts.checkItemId equals ci.id join it in this.dmdc.itTable on ci.typeId equals it.id select new NewClass { CheckTemplateId = ct.id, TypeId = ci.typeId, TypeName = it.typeName, ItemName = ci.itemName, StandardScore = ci.standardScore, StandardContent = ci.standardContent, Requirement = ci.requirement, } ; this.dataGrid1.ItemsSource = result.ToList();