Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm searching for a solution where i can fill a treeview in my WinForm from a datatable that has no parent and child ID's but is sorted the right way in the query. The first column is the parent and the second column is the child. Column 3 is the child from the second column etc. duplicate values need to be filtered out. The datatable is filled with a query from a sql database.

Table
Column1     Column2     Column3
A.............1...............1
A.............2...............1
A.............2...............2
B.............1...............2


Tree
A-1-1
...-2-1
.....-2
B-1-2


Thanks everybody
Posted
Updated 1-Aug-13 23:12pm
v4
Comments
Maciej Los 2-Aug-13 5:04am    
WinForms, WebControls?
DannyIO 2-Aug-13 5:08am    
It's for a WinForm, Thanks
David_Wimbley 4-Aug-13 1:56am    
The quick and dirty way would probably be to loop over the contents of your data table and add the nodes programmatically.

So you will first need to set a root node as you don't have one

rootNode = treeView1.Nodes.Add("Root Node");

Then you would say something like

If reader.read == column1 && reader.read == "A"
subNode = new TreeNode(reader.Read("Column2");
rootNode.Nodes.Add(subNode);

Then for your filtering out of data it would be something like

If reader.read == "column1" && reader.read == "A" && reader.read == "2"
subNode = new TreeNode(reader.Read("Column3");
subNode.Nodes.Add(subSubNode);

Something along those lines.

I did not post this as solution as it is merely meant to give you an idea to steer you in a particular direction. Although it would be easier to not use a data table and just adjust the queries you are running to get data out of SQL since the format you've given is providing a difficult constraint to work with

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900