我有一个UserControl包含DevExpress ComboBoxEdit控件,SearchLookUpEdit作为编辑器集。我已经在Designer中设置了searchLookUpEditView及其列。
我想从容器控件中设置UserControl的DataSource,DisplayMember和ValueMember属性,但在运行时我得到错误
"Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data"
但如果我使用此代码,为容器中的ComboboxEdit设置DataSource,而不是在UserControl中 - 那么一切正常
//_context is Entity Framework's ObjectContext
//workUnit1 is UserControl, that contains ComboBoxEdit control
var mod3 = (from a in _context.Darbi
select new { a.ID, a.pieteikuma_Nr, a.Nosaukums, title = a.pieteikuma_Nr + " " + a.Nosaukums });
workUnit1.DataSource = mod3;
workUnit1.ValueMember = "ID";
workUnit1.DisplayMember = "title";
[ComplexBindingProperties("DataSource", "DisplayMember")] //UserControl's attributes
public partial class WorkUnit: UserControl
{
//DataBinding parameters from container control
private object _dSource; //dataSource
private string _dMember; //displayMember
private string _vMember; //valueMember
//..//
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
get { return _dSource; }
set
{
if (value != null)
{
_dSource = value;
//workNameCmbBox is ComboBoxEdit control that i need to set SearchLookUpEdit from parent control
workNameCmbBox.DataBindings.Add("EditValue", _dSource, "ID");//The error is thrown at this line...
}
}
public string DisplayMember
{
get { return _dMember; }
set
{
_dMember = value ?? "";//ja null, tad ""
workNameCmbBox.Properties.DisplayMember = _dMember;
}
}
public string ValueMember
{
get { return _vMember; }
set {
_vMember = value ?? ""; //ja null, tad ""
workNameCmbBox.Properties.ValueMember = _vMember;
}
}
}
有什么建议吗?
答案 0 :(得分:1)
您无法直接绑定到查询结果,但需要填充一些本地集合。要完成此任务,您可以使用以下代码:
var list = mod3.ToList();
workUnit1.DataSource = list;
甚至以下代码:
修改强>
BindingList<T> bList = new BindingList<T>();
//
//...populate bList
//
BindingSource bSource = new BindingSource();
bSource.DataSource = bList;
//...set DataSource for usercontrol
workUnit1.DataSource = bSource;