C# 用列表填充Datagridview的一列

C# 用列表填充Datagridview的一列,c#,datagridview,C#,Datagridview,我有一个绑定到数据库的datagridview,我有bindingSource和bindingNavigator 因此,如果我想在本例中显示所有表livre,我编写以下代码: query = from x in ctx.livre select x; livreBindingSource.DataSource = query.ToList(); 我在dataGridView中添加了一个名为Hierarchy的列,以便添加列表中不在livre表中的一些信息 因此,我想将此列表绑定

我有一个绑定到数据库的datagridview,我有bindingSource和bindingNavigator

因此,如果我想在本例中显示所有表livre,我编写以下代码:

query = from x in ctx.livre
        select x;
livreBindingSource.DataSource = query.ToList();
我在dataGridView中添加了一个名为Hierarchy的列,以便添加列表中不在livre表中的一些信息

因此,我想将此列表绑定到该列层次结构

没有列层次结构的示例:

列层次结构示例:

我怎样才能做到这一点。
谢谢

更改您的选择以在linq中添加列

比如:

如果层次结构数据位于另一个列表中,则可以直接加入该列表,然后引用

var result = from y in ctx.Livre
                     join h in hierList on y.id equals h.hierId
                     select new { y.id, y.name, Hierarchy = h.hierString };
好的,在这里添加一些代码以显示平坦的层次结构

public class Rayon
    {
        public int idLocation { get; set; }
        public string LocationName { get; set; }
        public List<Rayon> idParentLocation { get; set; }
    }

    public class Livre
    {
        public int id { get; set; }
        public string name { get; set; }
        public string author { get; set; }
        public List<Rayon> Location { get; set; }
    }

List<Rayon> library = new List<Rayon>();
        library.Add(new Rayon() { idLocation = 1, idParentLocation = null, LocationName = "BookCase1" });
        library.Add(new Rayon() { idLocation = 10, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf1" });
        library.Add(new Rayon() { idLocation = 11, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf2" });
        library.Add(new Rayon() { idLocation = 12, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf3" });
        library.Add(new Rayon() { idLocation = 2, idParentLocation = null, LocationName = "BookCase2" });
        library.Add(new Rayon() { idLocation = 20, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf1" });
        library.Add(new Rayon() { idLocation = 21, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf2" });
        library.Add(new Rayon() { idLocation = 22, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf3" });
        library.Add(new Rayon() { idLocation = 3, idParentLocation = null, LocationName = "BookCase3" });
        library.Add(new Rayon() { idLocation = 30, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf1" });
        library.Add(new Rayon() { idLocation = 31, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf2" });
        library.Add(new Rayon() { idLocation = 32, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf3" });

        List<Livre> bkList = new List<Livre>();
        bkList.Add(new Livre() { id = 1, name = "Catch-22", author = "Heller", Location = new List<Rayon>() { library.Find(i => i.idLocation == 30) } });

        var test = bkList.SelectMany(b => b.Location.SelectMany(c => c.idParentLocation.Select(p => new { id = b.id, name = b.name, author = b.author, hierarchy = p.LocationName + "->" + c.LocationName + "->" + b.name })));

我没有h.hierId和h.hierString,你是怎么做到的@Kevin您的层次结构的业务规则是什么?如果我知道规则,我可以用密码告诉你。数据库中有父列吗?我有一个数据库librarie,它有两个表livre和rayon shelf。livre contain:id,name,author,isbn,idRayon人造丝contain:idRayon,name,idParent idParent是一个idRayon我列出所有表格livre by:query=from x在ctx中。livre选择x;livreBindingSource.DataSource=query.ToList;所以我想为每个livre添加它的人造丝和它的父对象,所以我添加了一个函数,返回一个包含smth的列表层次结构,如下所示:parent1_parent1_shelf1,parent1_parent1_shelf2。。。因此,我向dgv添加了一列,用于将列表添加到dgv@KevinDataGridView将类似于:1 | name1 | author1 | parent1->parent1->shelf1\n 2 | name2 | author1 | parent1->parent3->shelf23 | name3 | author2 | parent3->parent1->shelf1。。。我很抱歉,因为我不知道该怎么做。参考通告是什么?也就是说,id 1有父id 2,id 2有父id 1?这将导致linq出现问题,否则我将编写一个返回层次结构的查询
select new { x.id, x.name, Hierarchy = (x.id == 1 ? "1 is for name1" : "this is second") };
var result = from y in ctx.Livre
                     join h in hierList on y.id equals h.hierId
                     select new { y.id, y.name, Hierarchy = h.hierString };
public class Rayon
    {
        public int idLocation { get; set; }
        public string LocationName { get; set; }
        public List<Rayon> idParentLocation { get; set; }
    }

    public class Livre
    {
        public int id { get; set; }
        public string name { get; set; }
        public string author { get; set; }
        public List<Rayon> Location { get; set; }
    }

List<Rayon> library = new List<Rayon>();
        library.Add(new Rayon() { idLocation = 1, idParentLocation = null, LocationName = "BookCase1" });
        library.Add(new Rayon() { idLocation = 10, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf1" });
        library.Add(new Rayon() { idLocation = 11, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf2" });
        library.Add(new Rayon() { idLocation = 12, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase1")) }, LocationName = "1Shelf3" });
        library.Add(new Rayon() { idLocation = 2, idParentLocation = null, LocationName = "BookCase2" });
        library.Add(new Rayon() { idLocation = 20, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf1" });
        library.Add(new Rayon() { idLocation = 21, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf2" });
        library.Add(new Rayon() { idLocation = 22, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase2")) }, LocationName = "2Shelf3" });
        library.Add(new Rayon() { idLocation = 3, idParentLocation = null, LocationName = "BookCase3" });
        library.Add(new Rayon() { idLocation = 30, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf1" });
        library.Add(new Rayon() { idLocation = 31, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf2" });
        library.Add(new Rayon() { idLocation = 32, idParentLocation = new List<Rayon>() { library.Find(i => i.LocationName.Equals("BookCase3")) }, LocationName = "3Shelf3" });

        List<Livre> bkList = new List<Livre>();
        bkList.Add(new Livre() { id = 1, name = "Catch-22", author = "Heller", Location = new List<Rayon>() { library.Find(i => i.idLocation == 30) } });

        var test = bkList.SelectMany(b => b.Location.SelectMany(c => c.idParentLocation.Select(p => new { id = b.id, name = b.name, author = b.author, hierarchy = p.LocationName + "->" + c.LocationName + "->" + b.name })));