受到答案here的启发,我重构了我的代码,通过LINQ在其initil填充之后将记录添加到我的通用列表中,然后尝试在组合框中选择分配了内容的添加项。列表。
对于某些上下文/概念化,组合框中会填充候选学生;但是,如果已经安排了正在显示的周,那么已经安排在该周的那个插槽中的学生将被插入到作为组合框的数据源之后的列表中。最后,如果这样的学生确实存在于组合框中,我的意图是选择那个(但如果需要更改指定的学生,其他人仍然可以在列表中使用)。
以下是代码:
private void PopulateBibleReadingComboBox()
{
int BIBLE_READING_TALK_TYPE = 1;
if (!System.IO.File.Exists(AYttFMConstsAndUtils.STUDENTS_FILENAME)) return;
if (null == studentsList) return;
string assignedStudentFirstname = string.Empty;
string assignedStudentLastname = string.Empty;
Student assignedStudent = null;
if (currentWeekSaved)
{
DateTime currentWeek = Convert.ToDateTime(comboBoxWeekToSchedule.SelectedValue);
AssignmentHistory ah = AYttFMConstsAndUtils.AssignmentHistList
.FirstOrDefault(i => i.WeekOfAssignment == currentWeek && i.TalkType == 1);
assignedStudentFirstname = AYttFMConstsAndUtils.GetStudentFirstNameForID(ah.StudentID_FK);
assignedStudentLastname = AYttFMConstsAndUtils.GetStudentLastNameForID(ah.StudentID_FK);
assignedStudent = new Student() {FirstName = assignedStudentFirstname, LastName = assignedStudentLastname, StudentID = ah.StudentID_FK};
}
List<Student> BRStudents =
studentsList.Where(h => h.EnrolledInAYttFM)
.Where(i => i.RecommendedNextTalkTypeID.Equals(BIBLE_READING_TALK_TYPE))
.OrderBy(j => j.WeekOfLastAssignment)
.ToList();
if (null != assignedStudent)
{
BRStudents.Add(assignedStudent);
}
comboBoxBR.DataSource = BRStudents;
comboBoxBR.DisplayMember = "FullName";
comboBoxBR.ValueMember = "StudentID";
if (null != assignedStudent))
{
comboBoxBR.SelectedIndex = comboBoxBR.Items.IndexOf(assignedStudent.FullName);
}
}
问题是虽然有条件的&#34; SelectedIndex / IndexOf&#34;到达了 行,并且assignStudent.FullName就是它应该是的, 现在已添加到列表中,然后是组合框,该项目不是选择该行:
comboBoxBR.SelectedIndex = comboBoxBR.Items.IndexOf(assignedStudent.FullName);
相反,comboBoxBR.SelectedIndex是-1(尽管同样,那个组合框中确实存在fullName)。
注意:&#34; FullName&#34;学生班的成员计算:
public class Student
{
public int StudentID { get; set; }
. . .
public string FirstName { get; set; }
public string LastName { get; set; }
. . .
public string FullName
{
get
{
return string.Format("{0} {1}", FirstName, LastName);
}
set { ; }
}
}
答案 0 :(得分:1)
列表由Student对象组成,但FullName是一个字符串。您需要查找整个Student对象。您需要将相关行更改为:
comboBoxBR.SelectedIndex = comboBoxBR.Items.IndexOf(assignedStudent);
答案 1 :(得分:1)
当您使用(char []){ "string" }
时,就好像已经将对象添加到集合中(并且以某种形式或方式可能就是引擎盖下的情况)。因此,只搜索items集合中的名称将失败。更多MCVE:
DataSource
然后,设置选择:
Students = new List<Student>();
Students.Add(new Student(7, "Zowie", "Halston"));
Students.Add(new Student(6, "Ziggy", "Watson"));
Students.Add(new Student(18, "Zalgo", "d'Artagnan"));
Students.Add(new Student(67, "Tabitha", "Black"));
Student luckyStudent = Students.First(w => w.FirstName == "Ziggy");
cbo1.DataSource = Students;
cbo1.DisplayMember = "FullName";
cbo1.ValueMember = "Id";
通常,在使用if (luckyStudent != null)
{
// set selected: (WORKS):
//cbo1.SelectedItem = luckyStudent;
// set Index of item (WORKS):
//cbo1.SelectedIndex = cbo1.Items.IndexOf(luckyStudent);
// set Index of item name (FAILS):
cbo1.SelectedIndex = cbo1.Items.IndexOf(luckyStudent.FullName);
}
时,我会尽量避免摆弄项目集合。如果您尝试在DataSource
添加或删除,则会被大吼。所以,为此,我会使用
items
您可以在集合中找到它们,但它可以帮助您找不到控件绑定的跟踪。