简体   繁体   中英

This causes two bindings in the collection to bind to the same property. Parameter name: binding error in c#?

I have tried to bind a datasource to a DevExpress.XtraEditors.LookupEdit at run-time. I tried this code, but am getting the following error:

This causes two bindings in the collection to bind to the same property. Parameter name: binding.

Here is my code:

// 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));

How can this error be fixed?

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.

Clear binding first and then add it to the control:

CBL_SelectCustomer.DataBindings.Clear();
CBL_SelectCustomer.DataBindings.Add("EditValue", binding_cust, "CustomerId");

绑定前试试这个:

CBL_SelectCustomer.DataBindings.Clear();

I know this is an old post but in my case I kept getting that error because I had been calling a form_load event in de forms constructor (under InitializeComponent(); , a mistake when copy pasting). That kept loading the form twice each time which isn't noticeable till you encounter funky behavior like this. If that's your case, deleting that this.Load += new System.EventHandler(Form1_Load) (or similar call) will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM