简体   繁体   English

如何将列表显示到C#Datagrid中?

[英]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: 我目前有一个名为lstTrackScores的“ TrackActivity”列表,例如:

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? 我事先不知道列表中有多少不重复的学生,但是我确实在运行时发现了每个学生可以参加的最大活动数量。如何在数据网格中显示以上信息,以使人数唯一的学生是行数(这里是3:芭芭拉,克里斯和丹尼斯),活动的数量(这里是3:杂耍,骑自行车,射箭)是列数吗?

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. 我想我想创建一个二维数组,其行数等于上述Count,列数等于3。然后,以某种方式,我将该数组显示给Datagrid,但我不知道该怎么做这个。 I'm using C# 2010 Express. 我正在使用C#2010 Express。

What you basically want is a DataMatrix where rows and columns are dynamically dependend on the data input. 您基本上想要的是一个DataMatrix ,其中行和列是动态依赖于数据输入的。

在此处输入图片说明

Theres also an appropriate answer on Stackoverflow which will help Dynamic data matrix WPF 在Stackoverflow上也有一个适当的答案,这将有助于动态数据矩阵WPF

You will have to build a 2d array and feed it into the Grid. 您将必须构建一个二维数组并将其输入到Grid中。

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

你可以用这个

           yourdatagrid.DataSource = yourlist; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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