samedi 18 avril 2015

Populating TreeListView from existing hierarchy list

I have a list of objects called Items, that I want to display in a Treelistview in a hierarchy. The objects look like this:



public class ListItem
{
public int ID;
public string LabelText;
public int ParentID;
public bool Checked;
}


The list order ensures that parents are always defined first. Now I want to iterate over this list and build my hierarchy in the Treelistview. When I dectect a parent in the object I can search the already created nodes on the ListView and search for the ID, but I was wondering if it is possible to create the TreeNodes dynamically with the ID as the reference, like so (bit of pseudo code);



foreach (ListItem Item in Items)
{
TreeNode {Item.ID} = new TreeNode()
{
Checked = Item.Checked,
Text = Item.LabelText
};

if (Item.ParentID == null)
DropDown.Nodes.Add({Item.ID});
else
{Item.ParentID}.SubItems.Add({Item.ID});
}

Aucun commentaire:

Enregistrer un commentaire