用List填充一个Datagridview's列

本文关键字:Datagridview 一个 填充 List | 更新日期: 2023-09-27 18:04:05

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

所以如果我想显示所有的表(在我的例子中),我写这段代码:

query = from x in ctx.livre
        select x;
livreBindingSource.DataSource = query.ToList();

我在dataGridView中添加了一个名为Hierarchy的列,以便添加一些不在表livre中的信息(包含在列表中)。

所以我想把这个列表绑定到那个列层次

示例(不包含Hierarchy列):

query = from x in ctx.livre
        select x;
livreBindingSource.DataSource = query.ToList();

id | name
1 | name1
2  | name2

示例(带列层次结构):

query = from x in ctx.livre
        select x;
livreBindingSource.DataSource = query.ToList();
//Some code here for binding list<string> to column Hierarchy
id | name | Hierarchy
1 | name1 | 1 is for name1
2  | name2 | this is second

我该怎么做呢?谢谢!

用List填充一个Datagridview's列

改变你的选择添加列在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 })));
相关文章: