根据DataGridView在C# [英] Create Treeview depending on DataGridView in C#

查看:92
本文介绍了根据DataGridView在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从tow数据库,db1,db2获取数据,现在我想为结果datagridview中的数据创建treeView;在datagrid视图中有:id,name,director,第一个记录是prim导演,这意味着他没有导演(他是所有者),每个记录没有其他记录或有更多的记录(子)有孙子等等,这个场景就像在这个页面:

当我使用这个片段后,一些:

I get data from tow database, db1,db2, then Now I want to create treeView for the data in the resulting datagridview; in datagrid view there are: id, name, director,the first record is the prim director, that mean he has not up director(he is owner), each record has no other record or has more records(child), and each child has grandchild and so on, this scenario Just Like in the this page: I want to create treeview (parent and child and grandchild and so on), depending on xml file when i used this snippet after some :

void setTree()
        {
            {
            foreach(DataGridViewRow dt in DataGridView1.Rows)
            { 
          var per = this.DataGridView1.Rows.Cast<DataGridViewRow>().Select(n => new person
                {
                    name = dt.Cells[0].Value.ToString(),
                    Sex = dt.Cells[1].Value.ToString(),
                    Status = dt.Cells[2].Value.ToString(),
                    child = dt.Cells[3].Value.ToString(),
                    id = dt.Cells[4].Value.ToString(),
                    father = dt.Cells[5].Value.ToString()
                }).ToList();
               var rootTreeNode = GetTree(per, "").First();.........(1)
               treeView1.Nodes.Add(rootTreeNode);
            }
          }
        }
               private TreeNode[] GetTree(List<person> per, string parent)
        {
            return per.Where(p => p.father == parent).Select(p =>
            {
                var node = new TreeNode(p.name);
                node.Tag = p.id;
                node.Nodes.AddRange(GetTree(per, p.id));
                return node;
            }).ToArray();
        }

现在,当我使用这个代码时, ,它说:附加信息:序列不包含任何元素。

Now, when I use this code, I get error at mark(1),it say:Additional information: Sequence contains no elements.

谢谢

推荐答案

在互联网上的几个阅读和尝试解决这个小问题后,我终于成功了。
这是解决方案:

after several readings in internet and attempts to solve this small problem, I successed finally. this is the solution:

{
.............
TreeNode tn = new TreeNode(this.DataGridView2.Rows[0].Cells[0].Value.ToString());//text
tn.Tag = this.DataGridView2.Rows[0].Cells[4].Value.ToString();// id
tn.Name = this.DataGridView2.Rows[0].Cells[5].Value.ToString();//directorid
                treeView1.Nodes.Add(tn);
                settree(tn);
            }
            public void settree(TreeNode ns)
        {
            foreach (DataGridViewRow dr in DataGridView2.Rows)
            {
                if (dr.Cells[5].Value.ToString() == ns.Tag.ToString())
                {
                    TreeNode tsn = new TreeNode(dr.Cells[0].Value.ToString());
                    tsn.Tag = dr.Cells[4].Value.ToString();
                    tsn.Name = dr.Cells[5].Value.ToString();
                    ns.Nodes.Add(tsn);
                    settree(tsn);
                }
            }
        }

受益于此代码。

这篇关于根据DataGridView在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆