Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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;


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;
}
Posted
Updated 4-Mar-11 19:04pm
v2

1 solution

hi,
you can do this way
XML
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;
}
 
Share this answer
 
Comments
Behrang_a 5-Mar-11 1:57am    
the problem is CertType would not be CertficateType any more.
the error is as follow :
Error 11 Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#4>' to 'System.Linq.IQueryable<datamanager.certificatetype>
dongavipul 5-Mar-11 2:57am    
Hi,
for that you have to create one model class in that you have to take this three property say CID,Name and Ename and use this class instead of "CertificateType"
Behrang_a 12-Mar-11 23:32pm    
There is no other solution, Thanks for you response

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900