2

新しい行がデータベースに書き込まれたときに自動的に更新するWinformsコンボボックスを取得しようとしています。WinformsとEFでのデータバインディング4.1コード最初

POCO EFクラス:

public class BaseSweep 
{ 
    public int BaseSweepId { get; set; } 
    //stuff removed for clarity 
} 

私はこのようにするBindingListを介してデータに結合しています:

public BindingList<BaseSweep> TopSweeps() 
{ 
    LocalDbContext.BaseSweep.Load(); 
    return LocalDbContext.BaseSweep.Local.ToBindingList();      

} 

private void BindSweepList() //called in Form_Load 
{ 
    comboBoxSweepIds.DataSource = _dataAccess.TopSweeps(); 
    comboBoxSweepIds.DisplayMember = "BaseSweepId"; 
    comboBoxSweepIds.ValueMember = "BaseSweepId"; 
} 

これは、初期結合のために正常に動作し、テーブルの現在のIDを示しています。新しい行がテーブルに追加されると、LocalDbContext.BaseSweep.Localの数は期待どおりに増加します。ただし、comboBoxSweepIdsは更新されません。私が間違っていることは何ですか?

答えて

0

マークWは私に正しい道を率い

private void BindSweepList() 
{    
    _sweepCollection = _dataAccess.TopSweeps(); 
    _dataAccess.CollectionChanged<CableSweepDebug>(new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Local_CollectionChanged)); 

    UpdateSweepsData(); 
} 

private void Local_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
    UpdateSweepsData(); 
} 

private void UpdateSweepsData() 
{ 
    if (comboBoxSweepIds.InvokeRequired) 
    { 
     Invoke(new UpdateSweepCount(UpdateSweepsData)); 
    } 
    else 
    { 
     var tmpBind = _sweepCollection.OrderByDescending(t => t.BaseSweepId).Take(100); 

     comboBoxSweepIds.DataSource = null; 
     comboBoxSweepIds.DataSource = tmpBind.ToList() ; 
     comboBoxSweepIds.DisplayMember = "BaseSweepId"; 
     comboBoxSweepIds.ValueMember = "BaseSweepId";     

    } 
} 
0

行を追加するたびにイベントを発生させ、バインドを呼び出す必要があります。 、最初のデータバインドで

//put an event handler on the collection 
public void CollectionChanged<T>(System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler) where T : class 
{ 
    LocalDbContext.Set<T>().Local.CollectionChanged += eventHandler; 
} 

フォームクラスでプライベートBindingList<T>を使用してください(_sweepCollection

イベントハンドラを設定します:

関連する問題