首页 > 数据库技术 > 详细

Linq to SQL 绑定 ComboBox

时间:2017-11-14 18:15:24      阅读:335      评论:0      收藏:0      [点我收藏+]

最近在练习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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!