2009-04-25 59 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你就可以捕捉到从数据库中的对象,然后用该值等更新我们知道