如何将 DisplayMember 和 ValueMember 添加到具有已分配数据源的单个组合框项

本文关键字:数据源 分配 单个 组合 DisplayMember ValueMember 添加 | 更新日期: 2023-09-27 18:35:38

我成功地将显示/值对添加到如下所示的组合框中:

List<Student> BRStudents =
        studentsList.Where(h => h.EnrolledInAYttFM)
            .Where(i =>     
    i.RecommendedNextTalkTypeID.Equals(BIBLE_READING_TALK_TYPE))
            .OrderBy(j => j.WeekOfLastAssignment)
            .ToList();
    comboBoxBR.DataSource = BRStudents;
    comboBoxBR.DisplayMember = "FullName";
    comboBoxBR.ValueMember = "StudentID";

。但是我随后(在某些情况下)想将另一个项目添加到comboBoxBR,一个在BRStudents列表中不存在的项目。如果我尝试这样做:

AssignmentHistory ah = AYttFMConstsAndUtils.AssignmentHistList
    .FirstOrDefault(i => i.WeekOfAssignment == currentWeek && i.TalkType == 1);
string fullName = AYttFMConstsAndUtils.GetStudentFullNameForID(ah.StudentID_FK);
comboBoxBR.Items.Add(fullName);

。我得到,"设置数据源属性时无法修改项目集合。

真的,我想做这样的事情:

comboBoxBR.Items.Add(fullName, ah.StudentID_FK);

有没有办法将两个结果(学生列表和单个作业历史记录)合并到字典或某个此类集合中,然后将其分配为 comboBoxBR 的数据源?

为了充分披露,以下是学生和作业历史记录的定义:

public class Student
{
    public int StudentID { get; set; }
    public int FamilyID { get; set; }
    public bool EnrolledInAYttFM { get; set; }
    public DateTime DateEnrolledOrHiatusAYttFM { get; set; }
    public bool GivesBibleReading { get; set; }
    public bool PresentsICRVBS { get; set; }
    public bool IsHouseholder { get; set; }
    public bool IsMale { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddr { get; set; }
    public DateTime WeekOfLastAssignment { get; set; }
    public int RecommendedNextTalkTypeID { get; set; }
    public int NextCounselPoint { get; set; }
    public string FullName => $"{FirstName} {LastName}";
}
public class AssignmentHistory
{
    public DateTime WeekOfAssignment { get; set; }
    public int TalkType { get; set; } 
    public int StudentID_FK { get; set; }
    public int AssistantID_FK { get; set; } 
    public int CounselPoint { get; set; }
    public bool HasBeenEmailed { get; set; }
    public bool SlipHasBeenPrinted { get; set; }        
}

如何将 DisplayMember 和 ValueMember 添加到具有已分配数据源的单个组合框项

首先想到的是将

元素添加到用作数据源的List<Students>本身,而不是将元素添加到组合的 Items 集合(如果已设置数据源,则无法添加),但这无法达到您的目标,因为没有适当的机制来通知 ComboBox 数据源具有新元素。除非将列表重新绑定到组合框,否则看不到添加的元素。为此,首先需要取消绑定以前的数据源,然后再次重新绑定列表。

// Assuming a lot about your student class, hope it's clear the intention
BRStudents.Add(new Student() { newName = "ANewStudent", ID = 1} );
comboBoxBR.DataSource = null;
comboBoxBR.DataSource = BRStudents;
comboBoxBR.DisplayMember = "FullName";
comboBoxBR.ValueMember = "StudentID";

但是,有一个更好的解决方案,它是类 BindingList(T)
此类能够刷新绑定其实例的对象。

所以当你第一次绑定组合时,你必须写

BindingList<Student> bl = new BindingList<Student>(BRStudents);
comboBoxBR.DataSource = bl;
comboBoxBR.DisplayMember = "FullName";
comboBoxBR.ValueMember = "StudentID";

当您想将新元素添加到您编写
的列表中时(如果需要,在全局范围内移动 BL)

bl.Add(new Student() { newName = "ANewStudent", ID = 1} );

文档指出,您应该调用 ResetBindings 来强制刷新控件,但这似乎并不总是必需的......