I’m working on WinForm app in VS2008, I have a sql table that is in the following order:
Region | Contract | Name
-------------------------
South | Miami | John
South | Miami | Peter
South | Calafornia| Brock
North | New York | Josh
I would like to display it in a treeview as follows:
South
--Miami
--John
--Peter
--Calafornia
--Brock
North
--New York
--Josh
I can get it done by reading the database table and making a datatable that looks like this:
ID | ParentID | Description
-----------------------------
1 | | South
2 | 1 | Miami
3 | 1 | Calafornia
4 | 2 | John
5 | 2 | Peter
6 | 3 | Brock
I would like to know if that is the only way to get the data from the table into a treeview or can someone help me with a more intunative method
You can also iterate through all the rows returned ordered alphabetically on each level field, and if a level field changes its value, create a new branch on that level.
You always keep the last row in memory and compare the fields from right to left.