码迷,mamicode.com
首页 > 其他好文 > 详细

gridview 绑定多个格式相同的数据源(数据查询合并)

时间:2015-01-16 09:54:28      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

在做项目时,要求在同一个GridView中同时显示不同分组中的前若干条数据

几个技术要点:

1、数据分组,本方法中未用group by

2、几个结果格式相同的查询合并绑定

3、取查询结果的前或后若干条数据

        public int BindLiftData()

        {

            int count = 0;

 

            using (MySqlConnection conn = new MySqlConnection(dbServer))

            {

                FrDData db = new FrDData(conn);

 

                List<int> ids = new List<int>();

                var item =  from i in db.Items

                            join p in db.ProgS on i.ProgID equals p.ID

                            where p.State == 1 && i.Type == "升程"

                            select new {i.ID};

 

                bool isFirst = true;

                int firstID =0;

                foreach (var i in item)

                {

                    if (isFirst)

                    {

                        firstID = i.ID;

                        isFirst = false;

                    }

                    else

                        ids.Add(i.ID);

                }

 

                var q = (from i in db.Items

                         join l in db.Lifts on i.ID equals l.ItemID

                         where i.ID == firstID

                         orderby l.ID descending

                         select new { i.ID, i.Name, l.Angle, i.Stole, l.Lift }).Take(3).ToList();

 

                foreach(var x in ids)

                {

                    var r = (from i in db.Items

                             join l in db.Lifts on i.ID equals l.ItemID

                             where i.ID == x

                             orderby l.ID descending

                             select new { i.ID, i.Name, l.Angle, i.Stole, l.Lift }).Take(3).ToList();

                    q.AddRange(r);

                }

                gvLift.DataSource = q;

                count = q.Count;         

            }

            return count;

        }

gridview 绑定多个格式相同的数据源(数据查询合并)

标签:

原文地址:http://www.cnblogs.com/xihong2014/p/4227658.html

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