Sri*_*ari 5 c# devexpress winforms
我试图将数据源绑定到DevExpress.XtraEditors.LookupEdit运行时.我试过这段代码,但收到以下错误:
这会导致集合中的两个绑定绑定到同一属性.参数名称:绑定.
这是我的代码:
// 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));
Run Code Online (Sandbox Code Playgroud)
如何解决这个错误?
Nir*_*ngh 12
每个Control一次只能有一个绑定.看起来您之前已经绑定了文本框,现在当您尝试重新绑定它时,它会抛出错误.您需要在添加新绑定之前清除旧绑定.
首先清除绑定,然后将其添加到控件:
CBL_SelectCustomer.DataBindings.Clear();
CBL_SelectCustomer.DataBindings.Add("EditValue", binding_cust, "CustomerId");
Run Code Online (Sandbox Code Playgroud)