473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

TreeView to database or at least DataTable

The main question how to fully traverse/circumvent through all TreeView, for transforming Tree structure into a Table structure.
I want to note that the TreeView may contain a different number of branches, with different depths and different names.
I found a lot of information how to populate TreeView e.g. from DataTable, but I didn’t found information how to recursively traverse/circumvent a TreeView and populate/fill data e.g. to DataTable.

As sample I have following TreeView:
Aaron
-Baldwin
--Caleb
---Dale
--Earl
-Fabian
Gabriel
-Harold
-Ian

Necessary to convert the TreeView into e.g. DataTable:
id | Name | ParentId
--------------------------------
1 | Aaron | null
2 | Baldwin | 1
3 | Caleb | 2
4 | Dale | 3
5 | Earl | 2
6 | Fabian | 1
7 | Gabriel | null
8 | Harold | 7
9 | Ian | 7
Feb 8 '14 #1
1 6944
Following code is work well:
Expand|Select|Wrap|Line Numbers
  1. private void buttonSave_Click(object sender, EventArgs e)
  2.         {
  3.             TraverseTreeView(treeView1);
  4.             string temp = String.Empty;
  5.             foreach (string str in name)
  6.                 temp += str + Environment.NewLine;
  7.             MessageBox.Show(temp);
  8.             name.Clear();
  9.         }
  10.         List<string> name = new List<string>();
  11.         private void TraverseTreeView(TreeView tview)
  12.         {
  13.             TreeNode temp = new TreeNode();
  14.             for (int k = 0; k < tview.Nodes.Count; k++)
  15.             {
  16.                 temp = tview.Nodes[k];
  17.                 name.Add(k+"\t"+temp.Text+"\tnull");
  18.                 for (int i = 0; i < temp.Nodes.Count; i++)
  19.                     visitChildNodes(temp.Nodes[i]);
  20.             } 
  21.         }
  22.         private void visitChildNodes(TreeNode node)
  23.         {
  24.             name.Add(node.Text);
  25.             for (int j = 0; j < node.Nodes.Count; j++)
  26.                 visitChildNodes(node.Nodes[j]);
  27.         }
But there is still several things that I don't know how to realize:
1. Do not use global variable (List<string> name) for collecting result;
2. Correctly collect data perhaps in DataTable, in format "1 | Aaron | null".
Please chech code below, and fix mistaks:
Expand|Select|Wrap|Line Numbers
  1. private void buttonSave_Click(object sender, EventArgs e)
  2.         {
  3.             DataTable dt = new DataTable();
  4.             dt = TraverseTreeView(treeView1);
  5.             string temp = String.Empty;
  6.             foreach (string str in dt)
  7.                 temp += str + Environment.NewLine;
  8.             MessageBox.Show(temp);
  9.             dt.Clear();
  10.         }
  11.         //List<string> name = new List<string>();
  12.         private DataTable TraverseTreeView(TreeView tview)
  13.         {
  14.             DataTable dt = new DataTable();
  15.             dt.Columns.Add("id", typeof(int));
  16.             dt.Columns.Add("Name");
  17.             dt.Columns.Add("ParentId", typeof(int));
  18.             TreeNode temp = new TreeNode();
  19.             for (int k = 0; k < tview.Nodes.Count; k++)
  20.             {
  21.                 temp = tview.Nodes[k];
  22.                 dt.Rows.Add(k, temp.Text, null);
  23.                 //name.Add(temp.Text);
  24.                 for (int i = 0; i < temp.Nodes.Counst; i++)
  25.                     dt.Rows.Add(i, visitChildNodes(temp.Nodes[i]).Text, i - 1);
  26.             }
  27.             return dt;
  28.         }
  29.         private DataTable visitChildNodes(TreeNode node)
  30.         {
  31.             //name.Add(node.Text);
  32.             DataTable dt = new DataTable();
  33.             dt.Columns.Add("id", typeof(int));
  34.             dt.Columns.Add("Name");
  35.             dt.Columns.Add("ParentId", typeof(int));
  36.             for(int j = 0; j < node.Nodes.Count; j++)
  37.                 dt.Rows.Add(j, visitChildNodes(node.Nodes[j]).Text, j - 1);
  38.             return dt;
  39.         }
Feb 11 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Hutty | last post by:
I have a table that has two columns, Parent and Child. I'm able to populate a IEControls treeview with the Parent column. On the same row as the Parent is the child in the Child colum. How do...
2
by: DJG | last post by:
I need some help with a TreeView control in VB.Net I've got a datatable with about 8000 rows. 500 are 'folders', and the other 7500 rows are 'jobs'. It's a logical representation of some data,...
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
2
by: Guy007 | last post by:
I have a DataTable that is passed to me from an external application. I would like to save this DataTable to my database (MS SQL server) using .net (c#). How can a NEW table be automatically...
1
by: Amita Singh | last post by:
Hi, How to retrieve data from database in data table?
1
by: RAB | last post by:
I created a DataTable with numerous columns and rows. I want to update with my DataBase with selected columns from my DataTable (but not all the columns of information). Assuming the columns of...
1
by: tazdiver | last post by:
Hi All, I am having an issue with an windows application made from VB.NET 2003 which is using an access database. Basically the problem is that when the user updates a field on the app from a...
1
by: joel | last post by:
hi guys... i am a student, i am studying and i am a newbie in VB.net and i am using SQL Server 2000. I have this datatable which i insert rows (may take up to 2 or more rows because i would...
1
by: gomathinayagam | last post by:
hai, am only beginer in c#... i am trying to connect database with webform. using a technique that the fields of the table and the controls in a form are named same...then i try get the controls...
0
by: otomatis | last post by:
Hi All, I got this error when updating database from datatable. {"Update unable to find TableMapping or DataTable 'Table'."} Here is my code (modified) : SqlCeDataAdapter da = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.