如何在树视图中绑定数据表中的数据? [英] How to bind data from datatable in treeview?

查看:138
本文介绍了如何在树视图中绑定数据表中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Asp.net C#中的treeview控件中绑定数据表中的数据。我有一个表Id和Name的表。我想用父母,子女和子女创建树视图,

例如父母是0,1,2 .... 9,0的孩子是00,01,02,03,孩子1是10,11,12,12 ..... 00的子孙是000,001,002 ...... 10个是101,102 ......

如何做到这一点没有父ID列?



请帮忙...

I want to bind data from datatable in treeview control in Asp.net C#. I have one table with columns Id and Name. And I want to create treeview with parent, child and subchild,
For example parents are 0,1,2....9 , childs of 0 are 00,01,02,03, , childs of 1 are 10,11,12,12.....Subchild of 00 is 000,001,002...... for 10 are 101, 102......
How to do that with out parentID column?

Please help...

推荐答案

你好,

我认为你应该将id列作为varchar变量并在for循环中交叉,将每个id的长度作为节点的层次级别,即length = 1将是根节点,length = 2子节点和长度= 3子子节点。可能你必须使用递归函数。
Hello,
I think you should work the id column as a varchar variable and cross it in a for loop, taking the length of each id as the hierarchical level of the node, ie length = 1 would root node, the length = 2 the child node and length = 3 subchild node. Possibly you must use recursive functions.


试试这个

http://www.codeproject.com/Articles/7884/Data-Binding-TreeView-in-C [ ^ ]


您好,您可以这样使用。





假设您有两列(ParentID和ChildID),您可以过滤子行并将其绑定到新的treenodes。







Hi you can use like this.


Suppose If you have two columns (ParentID and ChildID), You can filter Child rows and bind it to the new treenodes.



private void AddNode(DataTable dt, string ChildID, TreeNode childnode)
     {

         DataRow[] dr = dt.Select("ParentID ='" + ChildID+ "');
         foreach (DataRow drChild in dr)
         {
             TreeNode childnode1 = new TreeNode();
             childnode1.Text = drChild["Name"];
             childnode.Nodes.Add(childnode1);
             AddNode(dtChild, drChild["ChildID"].ToString(), childnode1);
       }
     }







-Thanks

Sharath




-Thanks
Sharath


这篇关于如何在树视图中绑定数据表中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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