码迷,mamicode.com
首页 > 数据库 > 详细

Linq to SQL 绑定 ComboBox

时间:2017-11-14 18:33:41      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:java   uri   model b   new   dig   www   obj   ros   表数   

最近在练习EF的过程中,实现将Linq返回的结果绑定到Combox时出错。

错误提示如下:

System.NotSupportedException:“Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.”

技术分享

折腾了许久,把查询的结果转换等,最终发现ComBoBox可以绑定Linq查询的结果,唯一的要求就将DataSource放在DisplayMember和ValueMember的后面。

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            //读取表数据并绑定Combox空间
            using (OrderDBContainer db = new OrderDBContainer())
            {
                CBox_User.DisplayMember = "Name";
                CBox_User.ValueMember = "Id";
                CBox_User.DataSource = from u in db.UserAccoutSet 
                                      orderby u.LastName
                                      select new { Name = u.LastName + ", " + u.FirstName, Id = u.Id };
            }
        }

打赏支付宝:

技术分享

Linq to SQL 绑定 ComboBox

标签:java   uri   model b   new   dig   www   obj   ros   表数   

原文地址:http://www.cnblogs.com/taotaozujinet/p/7833705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!