I have some serious problems with populating a treeview recursively and I'll apreciate any help.
I have this DataTable:
ItemID ItemDesc Project Room Closet Item1 Item1Desc Project1 RoomE Closet-7 Item2 Item2Desc Project1 RoomW Closet8 Item3 Item3Desc Project1 RoomW Closet8 Item4 Item4Desc Project1 RoomN Closet2 Item5 Item5Desc Project1 RoomN Closet9 Item6 Item6Desc Project2 RoomN Closet2 Item7 Item7Desc Project2 RoomW Closet9
I want to create a TreeView like this:
- Project1
- RoomE
- Closet7
- Item1Desc
- Closet7
- RoomW
- Closet8
- Item2Desc
- Item3Desc
- Closet8
- RoomN
- Closet2
- Item4Desc
- Closet9
- Item5Desc
- Closet2
- RoomE
- Project2
- RoomN
- Closet2
- Item6Desc
- Closet2
- RoomW
- Closet2
- Item7Desc
- Closet2
- RoomN
The way I'm trying to databind the treeview is using
private List<treeDataItem> treeData = new List<treeDataItem>();
mytreeView.DataFieldID = "ID";
mytreeView.DataFieldParentID = "ParentID";
mytreeView.DataTextField = "Text";
mytreeView.DataValueField = "Value";
mytreeView.DataSource = treeData;
mytreeView.DataBind();
I'm trying to loop thru the DataTable so I can populate treeData but i can't enter the ParentIDs correctly.
Also, to make things complicated the number of fields in the DataTable are variable.
Which means that I can have more fields that identify an item like a "Closet Drawer" and then maybe a "Closet Drawer Section", etc.
So the DataTable can one time be like:
ItemID ItemDescription Project Room Closet Closet Drawer
And another time like:
ItemID ItemDescription Project Room Closet Closet Drawer Closet Drawer Section
based on user selection.
Here's the treeDataItem class:
internal class treeDataItem
{
public string Text { get; set; }
public int ID { get; set; }
public int ParentID { get; set; }
public Guid Value { get; set; }
public treeDataItem(int id, int parentId, string text, string value)
{
ID = id;
ParentID = parentId;
Text = text;
Value = value;
}
}