【问题标题】:ComboBox and Linq to Sql for Winforms用于 Winforms 的 ComboBox 和 Linq to Sql
【发布时间】:2010-10-21 20:23:58
【问题描述】:

我将 ComboBox 绑定到 BindingSource,而后者又绑定到 Linq Table。

此外,ComboBox 是从作为 Linq 表的数据源填充的。

每当用户选择 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(); ?还是有更好的办法?

提前致谢。

【问题讨论】:

    标签: .net winforms linq-to-sql data-binding combobox


    【解决方案1】:

    我会做类似的事情

    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,您都可以从数据库中捕获对象,然后使用值等更新它。

    让我们知道

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 2011-07-18
      • 2010-11-22
      相关资源
      最近更新 更多