ListBox中的DisplayMembers - 不显示任何内容

时间:2017-06-08 10:03:37

标签: c#

Form1中,为什么myListBoxname课程中显示我的Student属性

Form1

private static List<Student> studentListHome = new List<Student>();

...

public void BindData()
        {
            if (studentListHome != null)
            {
                studentListBox.DataSource = studentListHome;
                studentListBox.DisplayMember = "name";
            }
        }

    private void refreshButton_Click(object sender, EventArgs e)
            {
                BindData();
            }

Student

    private string name;

    public string Name
            {
                get
                {
                    return name;
                }
            }
...

public Student(string _name, int _id, string _bday)
        {
            name = _name;
            id = _id;
            bday = _bday;
        }

2 个答案:

答案 0 :(得分:0)

因为'name'是私人字符串。

尝试使用属性的“名称”。

studentListBox.DisplayMember = "Name";

答案 1 :(得分:0)

这解决了它

更改

studentListBox.DisplayMember = "Name";

foreach(Student s in studentListHome){
    studentListBox.DisplayMember = "Name";
}