I have a开发者_运维百科n List<Examination>
datasource where each record contains two objects. I want to bind datakeynames property with the pkIds of both objects e.g.
<asp:GridView DataKeyNames="obj1.pkId, obj2.pkId" ... />
I am binding the grid programatically. but I get error indicating:
DataBinding: 'objectList' does not contain a property with the name 'obj1.pkId'
Way around will be much appreciated.
Edit
class Examination
{
public string name{get;set;}
public TestClass test{get;set;}
public Subject subject{get;set;}
}
class TestClass
{
public int testId{get;set;}
}
class Subject
{
public int subjectId{get;set;}
}
Expected DataKeyNames are "test.testId, subject.subjectId" which makes Examination class objects unique.
My ObjectDataSource would be bound to List<Examination>
I had the same problem. Using the index value as described in the comments above by deostroll did not work.
I found the solution to my problem here instead.
I chose to implement a property at the root object level to represent the id of the child object.
To match the example above, the code would look like this:
class Examination
{
public string name{get;set;}
public TestClass test{get;set;}
public Subject subject{get;set;}
public int TestID { get { return test.testId; } }
public int SubjectID { get { return subject.subjectId; } }
}
class TestClass
{
public int testId{get;set;}
}
class Subject
{
public int subjectId{get;set;}
}
Then, to set the datakeys on my gridview in the markup of the grid, DataKeyNames="name, TestID, SubjectID".
精彩评论