0

次のようなコンボボックスに表示/値のペアを正常に追加しています。

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";

...しかし、(場合によっては) BRStudents リストにない別の項目をomboBoxBR に追加したいと考えています。私がこれをやろうとすると:

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

...「DataSource プロパティが設定されている場合、Items コレクションは変更できません。」

本当に、私は次のようなことをしたいです:

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

2 つの結果 (Student のリストと単一の AssignmentHistory) を辞書またはそのようなコレクションに結合し、それをコンボボックス BR の DataSource として割り当てる方法はありますか?

完全な開示のために、ここに Student と AssignmentHistory の定義を示します。

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; }        
}
4

1 に答える 1

1

コンボの Items コレクションに要素を追加する代わりに (DataSource を設定した場合は不可能)、最初に頭に浮かぶのは、List<Students>DataSource として使用されるそれ自体に要素を追加することですが、これは目的に達しません。 DataSource に新しい要素があることを ComboBox に通知するためのメカニズムが用意されています。リストをコンボボックスに再バインドしない限り、追加された要素は表示されません。これを行うには、最初に以前の DataSource をバインド解除してから、リストを再度バインドする必要があります。

// 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 を呼び出す必要があると記載されていますが、これは必ずしも必要ではないようです...

于 2016-02-21T17:00:51.163 に答える