1

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.

Community
  • 1
  • 1
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
  • 1
    Do you mean save to DataTable or to DataBase? Anyway, what structue may this datatable have. According to your exmple may this dataTable have one column (type string) or shall this table have multiple (dynamically created) columns (type string). Please add some more information! – Pilgerstorfer Franz Nov 30 '11 at 07:36
  • Read this: http://stackoverflow.com/questions/192220/what-is-the-most-efficient-elegant-way-to-parse-a-flat-table-into-a-tree/192462#192462 – Gert Arnold Nov 30 '11 at 07:50
  • Hippo generally in these cases you have a table with lets say ID, caption and ParentID and each non root node has parentID pointing to its parent node record in the table. I think you should go this way and not with a table like the one you have defined in your question. also consider that there are treeview controls able to populate exactly in this way when you bind them to such table structure like I mentioned. – Davide Piras Nov 30 '11 at 07:50
  • Edited the question, have a look. – Sangram Nandkhile Nov 30 '11 at 07:50
  • After your edit: your point of departure is still not clear. What is the source of your data? Do you already have hierarchical data stored somewhere and you just want to create a `System.Data.DataTable` dynamically? But then, what do you mean by "save it in the database"? – Gert Arnold Nov 30 '11 at 09:27
  • i want to display data in tabular form & thats why i need to save it in structure like datatbale or dataset so that i can show it to the user in a DataGrid. In future i might want to convert this datatable into database but for the time being showing it in the DataGrid will fulfill my requirement. Thanks. – Sangram Nandkhile Nov 30 '11 at 09:33

2 Answers2

1

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);
   }
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
0

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.

Piotr Auguscik
  • 3,651
  • 1
  • 22
  • 30