我试图在运行时将数据源绑定(bind)到 DevExpress.XtraEditors.LookupEdit
。我试过这段代码,但出现以下错误:
This causes two bindings in the collection to bind to the same property. Parameter name: binding.
这是我的代码:
// Create an adapter to load data from the "Customers" table.
OleDbDataAdapter testcustomers = new OleDbDataAdapter(
"SELECT CustomerId, Customername FROM Customer WHERE CompanyId =" + TXE_CompId.Text, connection);
DataSet ds = new DataSet(); // Create a dataset that will provide data from the database.
testcustomers.Fill(ds, "Customer"); // Load data from the "Customers" table to the dataset.
// A BindingSource for the "Customers" table.
BindingSource binding_cust = new BindingSource(ds, "Customer");
CBL_SelectCustomer.DataBindings.Add("EditValue", binding_cust, "CustomerId"); // getting error on this line
// Specify the data source to display in the dropdown.
CBL_SelectCustomer.Properties.DataSource = binding_cust;
// The field providing the editor's display text.
CBL_SelectCustomer.Properties.DisplayMember = "CustomerName";
// The field matching the edit value.
CBL_SelectCustomer.Properties.ValueMember = "CustomerId";
// Add two columns to the dropdown.
LookUpColumnInfoCollection coll = CBL_SelectCustomer.Properties.Columns;
// A column to display the ProductID field's values.
coll.Add(new LookUpColumnInfo("CustomerName", 0));
如何修复这个错误?
最佳答案
Every Control can have only one binding at a time. It looks like you already have a binding to the textboxes before and now when you try to rebind it, it throws an error. You need to clear the old binding before adding a new one.
先清除绑定(bind)再添加到控件中:
CBL_SelectCustomer.DataBindings.Clear();
CBL_SelectCustomer.DataBindings.Add("EditValue", binding_cust, "CustomerId");
关于c# - 这会导致集合中的两个绑定(bind)绑定(bind)到同一个属性。参数名称 : binding error in c#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23236318/