Hi There,
Firstly I would like to apologise if this has already been asked and answered, but i have spent a couple of hours cruising through the forums looking for an answer to my question.
My problem is that I have a collection of entities, which are bound to the grid columns. Then for each entity in the collection, there is another collection, that i want to dynamically create columns for and bind to the grid. So for example I have a list of students and then for each student I have a list of the subjects the attend. The number of subjects is not neccesarily the same for each student either.
So my columns at the top would look something like:
First Name | Second Name | Subject 1 | Subject 2 | Subject 3
Is this possible?
Hi,
So, i'm not exactly sure what you're looking to do.
But, you can create columns dynamically. However, all columns with the exception of our UnboundColumns must correspond to a property in your underlying data object.
So i suppose for each Subject Column you'd need to have an UnboundColumn that corresponds with it. Then you'd write a custom ValueConverter for each UnboundColumn that you create, that will look at the data, and display the correct value for it.
Hope that helps,
-SteveZ
Hi Steve,
Sorry I wasn't more clear. Basically i have the following:
Public Class Student{ public string Name { get; set; } public IEnumerable<Subject> { get; set; }}
Public Class Subject{ public string SubjectName { get; set; } public string Grade { get; set; }}
in my ViewModel I have an ObservableCollection<Student> that i want to render in the Grid.
What I would like to be able to do is effectively de-normalize the student and subjects into a row for each student (with 1 column for their Name), but then have dynamic columns for each of the subjects that they are enrolled in and show the grade as the content of the cell.
Does that make anymore sense? What you have said kind of makes sense to me, but I'm struggling to make the link between what you have said and an implementation.
thanks for your help, J
Thanks for the more specific information.
Ok, i think there area really 2 ways in which you can do this.
1. The xamGrid, as of (10.3) supports indexers. so you could do something like : grid.Columns.Add(new TextColumn(){Key=Subjects[0].Grade]}.
The problem with this approach, is that this will display the grade for Subject[0] for every student, regardless if its the same grade. However, i'm assuming you'd want to display the actual subject name. If thats the case, then you'd need to do use step 2
2. use the way i specified above, an example of the code would be like the following:
grid.Columns.Add(new UnboundColumn(){Key="Calc 2", ValueConverter= new SubjectConverter("Calc2"));
public class SubjectConverter : IValueConverter{ public SubjectConverter(string name){ this.SubjectName = name};
Then in the Convert method of your SubjectConverter it'd look like
Student student = (Student)value;
foreach(Subject subject in student.Subjects){ if(subject.Name == SubjectName) return subject.Grade;}Hope this helps, -SteveZ