问题描述
我的LINQ中有两个表:第一个是CertificateType(CID,Name,EID),第二个是Equipment(EID,EName),这些表通过"EID"列具有关系.
当我创建一个CertificateType对象时,我可以简单地检索相关的设备名称:
I have two tables in my LINQ: first one is CertificateType (CID,Name,EID) and second one is Equipment(EID,EName), tables have a relation via "EID" column.
When i create a CertificateType object I can simply retrieve the related Equipment Name :
CertificateType Cert = new CertificateType();
string EName = Cert.Equipment.EName;
我动态创建了一个gridview并将其绑定到CertificateType表,问题是如何将Equipment.EName列绑定到我的网格视图?
这是我的GridView代码,在尝试绑定Equipment.Ename时抛出异常:
I made a gridview dynamically and bind it to CertificateType table , the problem is how I can bind Equipment.EName column to my grid view?
here is my GridView code which throw an exception when trying to bind Equipment.Ename :
IQueryable<CertificateType> CTList = CertificateTypeSearch();
GridView1.DataSource = CTList;
//Setting DataKeyNames
GridView1.DataKeyNames = new string[] { "CID", "Name"};
//Binding Data by creating BoundField
BoundField Name = new BoundField();
Name.DataField = "Name";
Name.HeaderText = "Name";
BoundField EquipmentName = new BoundField();
EquipmentName.DataField = "Equipment.EName";
EquipmentName.HeaderText = "Equipment Name";
GridView1.Columns.Add(Name);
GridView1.Columns.Add(EquipmentName);
GridView1.DataBind();
public static IQueryable<CertificateType> CertificateTypeSearch()
{
DataClassesDataContext context = new DataClassesDataContext();
var CertType = (from certificateType in context.CertificateTypes
select certificateType);
return CertType;
}
推荐答案
public static IQueryable<CertificateType> CertificateTypeSearch()
{
DataClassesDataContext context = new DataClassesDataContext();
var CertType = (from certificateType in context.CertificateTypes
select new
{
CID = certificateType.CID,
Name = certificateType.Name,
EName = certificateType.Equipment
});
return CertType;
}
这篇关于动态将多个表LINQ绑定到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!