简体   繁体   中英

How do I display my List into a C# Datagrid?

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.

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

在此处输入图片说明

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.

将查询结果转换为通用列表,并将其作为数据源传递给datagrid。

你可以用这个

           yourdatagrid.DataSource = yourlist; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM