c# - 这会导致集合中的两个绑定(bind)绑定(bind)到同一个属性。参数名称 : binding error in c#?

标签 c# winforms devexpress

我试图在运行时将数据源绑定(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/

相关文章:

c# - 有条件地加入 LINQ?

c# - 使用 System.Net.Http.HttpClient 的并行 HTTP 请求

c# - 在 .NET Windows 窗体中,如何在两个 EXE 或应用程序之间发送数据?

devexpress - 如何将文本框值从一个网络表单传递到 xtrareport?

.net - 没有XPO的DevExpress的服务器端分页

c# - 如何在 xamarin.mac 中绘制矩形

c# - SaveChanges() 正在保存 Unchanged 记录

c# - 不可点击的上下文菜单标题

c# - 在 Windows 7 中更改屏幕保护程序设置时应用程序卡住 - System.Threading.Timer 是罪魁祸首?

mysql - 关于更新 MySQL 数据库和我的 devexpress Gauge 值之间的数据问题的数据绑定(bind)问题?