Converting TreeNode to DataTable Dynamically

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.

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.

Answers


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);
   }
}

Need Your Help

2 RewriteRule in htaccess

.htaccess

I would like to redirect everything to a.php. And baba to b.php.

Convert javascript function to Obj-C function

javascript objective-c function

I want to use a bunch of functions that were written in Javascript in my Obj-C app. I made a header file and got the first one converted, but got stuck on the second. Here's what I started with and