【问题标题】:The multi-part identifier "System.Data.DataRowView" could not be bound [duplicate]无法绑定多部分标识符“System.Data.DataRowView”[重复]
【发布时间】:2013-04-18 05:48:16
【问题描述】:

我有一个填充 Combobox1 的表,而 Combobox1 应该填充 Combobox2,这就是问题所在。 这是我得到的例外

无法绑定多部分标识符“System.Data.DataRowView”。

代码:

    private void frm2_Load(object sender, EventArgs e)
    {
        //Populate Combobox1
        SqlDataAdapter da = new SqlDataAdapter("SELECT CategoryID, Name FROM Categories", clsMain.con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        comboBox1.DataSource = ds.Tables[0];
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "CategoryID";
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //populate Combobox2
        SqlDataAdapter da = new SqlDataAdapter("SELECT SubCategoryID, Name FROM SubCategories WHERE CategoryID=" + comboBox1.SelectedValue, clsMain.con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        comboBox2.DataSource = ds.Tables[0];
        comboBox2.DisplayMember = "Name";
        comboBox2.ValueMember = "SubCategoryID";
    }

【问题讨论】:

    标签: c# sql-server ado.net


    【解决方案1】:

    这是由于在第一个组合中填充数据时加载了第二个组合框。
    您可以通过以下方式避免此错误:
    1. 使用 SelectionChangeCommitted 事件而不是 SelectedIndexChanged 事件。
    或者
    2. 分离选定的索引更改事件,填充组合框,再次附加事件为:

    private void frm2_Load(object sender, EventArgs e)
    {
       //Detach event
       comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
    
        //Populate Combobox1
        SqlDataAdapter da = new SqlDataAdapter("SELECT CategoryID, Name FROM Categories", clsMain.con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        comboBox1.DataSource = ds.Tables[0];
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "CategoryID";
    
       //Attach event again
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
    }
    

    希望这会对你有所帮助。

    【讨论】:

    • 我没想到会那么容易。两种方法都很好,我非常感谢你的帮助,这非常有帮助..谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    相关资源
    最近更新 更多