首页 > 代码库 > gridview 绑定多个格式相同的数据源(数据查询合并)

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

在做项目时,要求在同一个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 绑定多个格式相同的数据源(数据查询合并)