2009-04-25 66 views
0

我有ComboBox綁定到BindingSource然後綁定到Linq表。組合框和Linq到Winforms的

另外,ComboBox從Linq Table的數據源中填充。

無論何時用戶在ComboBox中選擇項目,我都希望爲BindingSource.Current的某個屬性分配新值。我從MSDN瞭解到我需要使用ComboBox的SelectionChangeCommitted事件。

這裏是僞代碼:

myCB_SelectionChangeCommitted(...) 
{ 
    var val = myCB.SelectedValue; //I get selected value from user. 
    var q = bindingSource.Current as XYZ; 
    //Ommitted validation code to check if q is okay to work with 
    q.SomeProperty = NewCalculatedValue; //SomeProperty is bound to label 
    //After above line, myCB.SelectedValue is lost! 
    //It seems that this event fires **before** selected value 
     //is written to bound data source 
} 

是否必須使用myCB.DataBindings [0] .WriteValue(); ?或者,還有更好的方法?

在此先感謝。

回答

0

我會做類似

long index = q.databaseindex; 
LinqObject = (from l in DataContext.linqtable where l.index = index select l).First(); 
    //Make sure you do a check that count > 0 before calling First! 
LinqObject.property = value; 
LinqObject.SubmitChanges(); 

這樣,不管對象是如何堅持到ComboBox你就可以捕捉到從數據庫中的對象,然後用該值等更新我們知道