Reputation: 49
I am Using LINQ to query a table that has a foreign key for another table, I have run SQLMetal and generated the classes for the tables (as I am using SQL Compact Edition I don't have much choice) and I am manually creating the columns for my grid view.
I have set the DataPropertyNames for each of the columns, and they work for the Properties of the parent objects, however I would like to populate a column with the value from one of the foreign tables (which in this case is called 'issues'), I have tried setting the DataPropertyName to 'issues.Title' however this has no effect and the column remains empty.
If I loop over the Query result and print the value of 'issues.Title' they are present, however they still do not show up in the column. Does DataPropertyName not support sub objects like this or is it due to LINQ? I have tried setting up my query to use a join against the foreign keys too but I have the same result.
Edit: I am aware that there is a 'toString' workaround that involves overiding the method on the nested object to return the value, however this isn't applicable here since I'd like access to more than one property.
After some searching it's parent that the DataGridView simply doesn't support it, so I am looking for any other way of accessing a nested object's property for use in the grid.
Upvotes: 0
Views: 1085
Reputation: 109185
It looks like you'll have to create delegating properties in your parent object like:
public string IssueTitle
{
get { return issues.Title; }
}
Etc.
Some even contend that this is the only proper way in terms of object oriented programming because it encapsulates child objects or referred objects in the parent object. Fact is, you can have the parent object itself decide what to render if e.g. there is no issues object.
EDIT You may also consider using a view model.
Upvotes: 1