Question

I have a classroom full of students and I've tracked which activity they've done and what score they achieved on each activity using this code:

public class TrackActivity
{
     public String StudentName {get; set;}
     public String ActivityName {get; set;}
     public int ActivityScore {get; set;}  // out of 10 points
}

I currently have a List of "TrackActivity" called lstTrackScores, such that:

Barbara, juggling, 9
Barbara, cycling, 7
Chris, cycling, 9
Dennis, juggling, 8
Dennis, cycling, 6
Dennis, archery, 10

I don't know in advance how many unique students are in my List, but I do find out at run-time the maximum number of activities each student can participate in. How do I display the above information in a datagrid such that the number of unique students act as the number of rows (here, 3: Barbara, Chris, and Dennis), and the number of activities (here, 3: juggling, cycling, archery) act as the number of columns?

Then, in each cell, there would be a score if the student participated in that activity, and a blank if they did not participate in that activity. Ultimately, I'd like to display the above in a datagrid without having to change my class.

I tried to determine the number of unique students by doing this:

lstTrackScores.Select(x => x.StudentNames).Distinct().Count();

I think I want to create a 2-dimensional array with number of rows equal to the Count above, and number of columns equal to 3. Then, somehow, I display that array to a Datagrid, but I don't know how to do this. I'm using C# 2010 Express.

Was it helpful?

Solution

What you basically want is a DataMatrix where rows and columns are dynamically dependend on the data input.

enter image description here

Theres also an appropriate answer on Stackoverflow which will help Dynamic data matrix WPF

You will have to build a 2d array and feed it into the Grid.

OTHER TIPS

convert the result of the query to a generic List and pass it as a datasource to datagrid.

You can use this

           yourdatagrid.DataSource = yourlist; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top