我有以下代码用通用列表的内容填充组合框,尝试使用 FirstName 和 LastName 的串联作为组合框的 DisplayMember:
private void PopulateStudentsComboBox()
{
if (System.IO.File.Exists(AYttFMConstsAndUtils.STUDENTS_FILENAME))
{
if (null == _studentsList)
{
var students = System.IO.File.ReadAllText(AYttFMConstsAndUtils.STUDENTS_FILENAME);
_studentsList = JsonConvert.DeserializeObject<List<Student>>(students);
}
// Sort by firstname
_studentsList = _studentsList.OrderBy(i => i.FirstName).ToList();
comboBoxStudents.DataSource = _studentsList;
string firstNameLastName = "FirstName LastName";
comboBoxStudents.DisplayMember = firstNameLastName;
comboBoxStudents.ValueMember = "StudentID";
}
}
这不起作用;ValueMember 成为 DisplayMember。
这有效:
. . .
comboBoxStudents.DataSource = _studentsList;
comboBoxStudents.DisplayMember = "FirstName";
comboBoxStudents.ValueMember = "StudentID";
. . .
...但不削减芥末 - 我需要给定和姓氏,而不仅仅是给定的名字(反之亦然)。
如何组合两个类字段来组成组合框的 DisplayMember?