多个部分的标识符"System.Data.DataRowView"被指定为"System.Data.DataRowView".不能被束缚 [英] The multi-part identifier "System.Data.DataRowView" could not be bound
问题描述
我有一个填充Combobox1的表,而Combobox1应该填充Combobox2,这就是问题所在. 那是我得到的例外
I have a table that populate Combobox1 and Combobox1 should populate Combobox2 and this is where the problem is. That's the exception i'm getting
无法绑定多部分标识符"System.Data.DataRowView".
The multi-part identifier "System.Data.DataRowView" could not be bound.
代码:
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";
}
推荐答案
这是由于在第一个组合中填充数据时加载了第二个组合框.
您可以通过以下方法避免此错误:
1.使用 SelectionChangeCommitted 事件而不是 SelectedIndexChanged 事件.
或
2.分离选定的索引更改事件,填充组合框,然后再次将事件附加为:
This is due to loading second combobox while filling data in first combo.
You can avoid this error by:
1. Use SelectionChangeCommitted event instead of SelectedIndexChanged event.
Or
2. Detach selected index change event, populate combobox, attach event again as:
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;
}
希望这会对您有所帮助.
Hopes this will help you.
这篇关于多个部分的标识符"System.Data.DataRowView"被指定为"System.Data.DataRowView".不能被束缚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!