请考虑我不擅长英语。
Class'Clele'列表绑定到ListBox,Displaymemeber是'group'字段。
我的目标是显示该组没有重叠。 (只是A和B)
但是我的代码显示如下。
我如何只显示A和B?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Student st1 = new Student("Kim", 15, Group.A);
Student st2 = new Student("Lee", 15, Group.A);
Student st3 = new Student("Park", 15, Group.B);
Student.Add(st1);
Student.Add(st2);
Student.Add(st3);
listBox1.DataSource = Student.LstStudent;
listBox1.DisplayMember = "group";
}
}
public enum Group
{
None,
A,
B,
C
}
public class Student
{
private static List<Student> _LstStudent = new List<Student>();
public static List<Student> LstStudent
{
get
{
return _LstStudent;
}
}
public Group group { get; set; }
public string name { get; set; }
public int age { get; set; }
public Student(string name, int age, Group group)
{
this.name = name;
this.age = age;
this.group = group;
}
public static void Add(Student student)
{
LstStudent.Add(student);
}
}
答案 0 :(得分:0)
如果您打算只显示群组,可以使用LINQ过滤List<Student>
。
尝试更改
listBox1.DataSource = Student.LstStudent;
listBox1.DisplayMember = "group";
要
listBox1.DataSource = Student.LstStudent.Select(x => x.group).Distinct().ToArray();