This code shows how to move from a hierarchical table to a TreeView. There is nothing complicated here, but you need to do a few tricks to make it work.
The first trick is to sort the entries using the ParentID. We cannot insert a node into the tree until its parent node is in the tree. This implies a special case where the root of the tree node must be inserted first, since it does not have a parent.
Here is an example of data:
// Create the DataTable and columns DataTable ItemTable = new DataTable("MyTable"); ItemTable.Columns.Add("ID" , typeof(int )); ItemTable.Columns.Add("ParentID", typeof(int )); ItemTable.Columns.Add("Name" , typeof(String)); // add some test data ItemTable.Rows.Add(new object[] { 0,-1, "Bill Gates" }); ItemTable.Rows.Add(new object[] { 1, 0, "Steve Ballmer" }); ItemTable.Rows.Add(new object[] { 3, 1, "Mary Smith" }); ItemTable.Rows.Add(new object[] { 2, 0, "Paul Allen" }); ItemTable.Rows.Add(new object[] { 4, 2, "Ahmed Jones" }); ItemTable.Rows.Add(new object[] { 5, 2, "Wing Lee" }); // Use the Select method to sort the rows by ParentID DataRow[] SortedRows; SortedRows = ItemTable.Select("", "ParentID");
Hope you find this helpful
CollectMeNow
source share