简体   繁体   English

如何更改显示在组合框中的名称?

[英]How can i change the Name that is Displayed in the Combobox?

I am building a Studentlist and don't know how to overcome a problem.我正在建立一个学生名单,但不知道如何解决问题。 I am using a Combobox to display all the Students that have been created.我正在使用组合框来显示已创建的所有学生。 I save the Students directly into the Combobox using this code:我使用以下代码将学生直接保存到组合框中:

private void btnSpeichern_Click(object sender, EventArgs e)
        {
            Student StudentSave = new Student
            {
                ID = txtStudentID.Text,
                FirstName = txtFirstName.Text,
                LastName = txtLastName.Text,
                Age = nudAge.Value,
                Height = nudHeight.Value,
                Schoolclass = txtSchoolClass.Text,
                Gender = cbxGender.Text,
            };

            cbxStudentIDs.Items.Add(StudentSave);
        }

cbxStudentIDs stands for the Combobox. cbxStudentIDs 代表组合框。 I would like to have the Student ID as the name that is displayed, but It shows "WindowsFormsApp2.Form1+Student" for every Student I save.我希望将学生 ID 作为显示的名称,但它为我保存的每个学生显示“WindowsFormsApp2.Form1+Student”。

I'm using Visual Studio 2019 C#.我正在使用 Visual Studio 2019 C#。 Thanks for any helpful advice!感谢您提供任何有用的建议!

You could begin by declaring a property of BindingList<Student> .您可以首先声明BindingList<Student>的属性。 For example,例如,

BindingList<Student> StudentCollection = new BindingList<Student>();

You could then bind this list to the ComboBox using the following.然后,您可以使用以下命令将此列表绑定到 ComboBox。

cbxStudentIDs.DataSource = StudentCollection;
cbxStudentIDs.DisplayMember = "ID";

The DisplayMember ensures that the ID property of Student is used as Display String for ComboBox. DisplayMember确保将 Student 的 ID 属性用作 ComboBox 的显示字符串。

You could now continue adding the Student to the newly created Collection as您现在可以继续将学生添加到新创建的集合中

Student StudentSave = new Student
            {
                ID = txtStudentID.Text,
                FirstName = txtFirstName.Text,
                LastName = txtLastName.Text,
                Age = nudAge.Value,
                Height = nudHeight.Value,
                Schoolclass = txtSchoolClass.Text,
                Gender = cbxGender.Text,
            };
 StudentCollection.Add(StudentSave);

BindingList<T> supports two-way databinding.This would ensure each time you add a new item to your collection ( StudentCollection ), the ComboBox would be refreshed accordingly. BindingList<T>支持双向数据绑定。这将确保每次向集合 ( StudentCollection ) 添加新项目时,ComboBox 都会相应地刷新。

You don't want to add the whole object to the combobox, just a list of IDs.您不想将整个对象添加到组合框,而只是一个 ID 列表。 So you would change cbxStudentIDs.Items.Add(StudentSave);所以你会改变cbxStudentIDs.Items.Add(StudentSave); to cbxStudentIDs.Items.Add(StudentSave.ID);cbxStudentIDs.Items.Add(StudentSave.ID);

You would then have to keep the actual state of the student object elsewhere that you can link to it using the ID, whether that is a database or an in memory collection is up to you.然后,您必须将学生对象的实际状态保留在其他地方,您可以使用 ID 链接到它,无论是数据库还是内存集合取决于您。

Alternatively, you can link a data source with the student data in as a source for the combobox and set the DisplayMember as detailed here或者,您可以将数据源与学生数据链接为组合框的源,并按照此处的详细说明设置DisplayMember

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

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