【问题标题】:WinForm Combobox - incorrect SelectedValue at Form loadWinForm 组合框 - 表单加载时 SelectedValue 不正确
【发布时间】:2012-07-02 04:27:41
【问题描述】:

我的 windows 窗体包含 1 个绑定到类别绑定源的组合框和 1 个绑定到产品绑定源的数据网格。当我加载表单时,组合框显示类别表中的第一个值,而不是 Products 表中的选定值,当我在 Products 绑定源中更改位置时,我可以在组合框中获得正确的 selectedValue(它显示正确的值)。所以我的问题在于第一次加载组合框项目。

我的组合属性:

数据源 = 类别绑定源

显示成员 = 类别名称

值成员 = CategoryID

选定值 = productBindingSource – CategoryID

这是我的代码:

NorthwindDataContext dc;
private void Form1_Load(object sender, EventArgs e)
{
    dc = new NorthwindDataContext();
    productBindingSource.DataSource = dc.Products;

    this.categoryIDComboBox.DataSource = dc.Categories;
}

【问题讨论】:

    标签: winforms linq-to-sql data-binding combobox lookup


    【解决方案1】:

    尝试颠倒组合框和数据网格的初始化顺序

    NorthwindDataContext dc; 
    private void Form1_Load(object sender, EventArgs e) 
    { 
        dc = new NorthwindDataContext(); 
        this.categoryIDComboBox.DataSource = dc.Categories; 
        productBindingSource.DataSource = dc.Products; 
    } 
    

    【讨论】:

    • 对了,Combo的初始化应该在datagrid之前,谢谢Steve。
    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 2013-02-23
    • 2013-08-09
    • 2017-08-20
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多