动态地将TreeNode转换为DataTable

发布于 2024-12-19 05:57:55 字数 792 浏览 2 评论 0原文

有很多方法可以将数据表转换为树视图(树节点),例如这个。但是有什么方法可以创建一个维护层次结构的数据表。对于前。如果 TreeNode 就像

A-A1
 -A2-A21
    -A22
B-B1
 -B2-B21
 -B3-B31
    -B32-B321
        -B322
    -B33
 -B4

数据表中的以下内容一样。然后我可以将其保存在数据库中或在datagrid中向用户显示。

在此处输入图像描述

所有列都可以采用 string 值。

有什么方法可以将 treenode 传递给函数 &它将动态创建这样的数据表。 treenode 结构每次都会改变,这就是为什么我不能对其进行硬编码。假设我有一个像这样的递归函数。

public void CreateData(TreeNode trn)
{

   foreach(TreeNode t in trn.Nodes)
   {
       CreateData(t);
   }

}

我在理解递归函数的代码流程方面面临困难。 任何建议表示赞赏。

谢谢。

There are plenty of methods to convert datatable into treeview (treenode) like this. But is there any way to do create a datatable maintaining hierarchy. For ex. if TreeNode is like

A-A1
 -A2-A21
    -A22
B-B1
 -B2-B21
 -B3-B31
    -B32-B321
        -B322
    -B33
 -B4

should look like following in datatable. Then i can either save it in the databse or show it to the user in a datagrid.

enter image description here

All column can have take string values.

Is there any way so i can pass a treenode to a function & it will create datatble like this dynamically. treenode structure will change everytime that's why i can not hardcode it. Suppose i have a recursive function like this.

public void CreateData(TreeNode trn)
{

   foreach(TreeNode t in trn.Nodes)
   {
       CreateData(t);
   }

}

i am facing difficulty in understanding flow of code for recursive function.
any suggestion is appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2024-12-26 05:57:55

这是一个伪代码,希望它能帮助您解决问题

private void getNodeList()
{
    //This will hold your node text
    List<string> nodeTextList = new List<string>();
    //loop through all teeview nodes
    foreach(TreeNode rootNode in treeView.Nodes)
    {
       //Add root node to list
       nodeTextList.Add(rootNode.Text);
       //Do recursive getter to get child nodes of root node
       getChildNodes(rootNode, nodeTextList);

    }
    //Do with nodeTextList what ever you want, for example add to datatable.
}

private void getChildNodes(TreeNode rootNode, List<string> nodeTextList)
{
   foreach(TreeNode childNode in rootNode.Nodes)
   {
       //Add child node text
       nodeTextList.Add(childNode.Text);      
       getChildNodes(childNode, nodeTextList);
   }
}

Here's a pseudo code, hope it will help you to deal with your problem

private void getNodeList()
{
    //This will hold your node text
    List<string> nodeTextList = new List<string>();
    //loop through all teeview nodes
    foreach(TreeNode rootNode in treeView.Nodes)
    {
       //Add root node to list
       nodeTextList.Add(rootNode.Text);
       //Do recursive getter to get child nodes of root node
       getChildNodes(rootNode, nodeTextList);

    }
    //Do with nodeTextList what ever you want, for example add to datatable.
}

private void getChildNodes(TreeNode rootNode, List<string> nodeTextList)
{
   foreach(TreeNode childNode in rootNode.Nodes)
   {
       //Add child node text
       nodeTextList.Add(childNode.Text);      
       getChildNodes(childNode, nodeTextList);
   }
}
不必在意 2024-12-26 05:57:55

如果您无法理解该函数或它的工作原理,您可能需要阅读深度优先搜索。这正是您正在做的+您应该添加代码来保存您当前所在的节点。

If you cann't understand that function or how it should work, you might want to read about Depth-first search. Its exacly what you're doing + you should add code that save the node you're in at moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文