0

我有一个包含 DevExpress ComboBoxEdit 控件的 UserControl,其中 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;
                }
            }
        }

有什么建议么?

4

1 回答 1

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;
于 2013-04-09T11:34:07.790 回答