I have a list of objects with properties. The object class shown below:
public class ElementImpression
{
public int ElementId { get; private set; }
public string FamilyAndTypeName { get; private set; }
public string CategoryName { get; private set; }
public int CategoryNumber { get; private set; }
public string SystemAbbreviation { get; private set; }
public ElementImpression(Element e)
{
ElementId = e.Id.IntegerValue;
FamilyAndTypeName = e.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).AsValueString();
CategoryName = e.Category.Name;
CategoryNumber = e.Category.Id.IntegerValue;
SystemAbbreviation = e.get_Parameter(BuiltInParameter.RBS_DUCT_PIPE_SYSTEM_ABBREVIATION_PARAM).AsString();
}
}
The goal is to parse the list and create a structured, hierarchical presentation in a TreeView control. The number of levels in the hierarchy and which properties to use as nodes is defined at runtime by the user. I have been succesful in creating the following treeview: Treeview by using the following code:
private void UpdateTreeView(object sender, MyEventArgs e)
{
//Level 0: All
//Level 1: System Abbreviation
//Level 2: Category Name
//Level 3: Family and Type Name
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
//Payload is a container object holding the list to be parsed. It is cached as a property in the form.
//Payload.ElementsInSelection is the list of objects to parse.
var lv1Group = Payload.ElementsInSelection.GroupBy(x => x.SystemAbbreviation);
treeView1.Nodes.Add("All");
int i = -1;
foreach (IGrouping<string, ElementImpression> group1 in lv1Group)
{
treeView1.Nodes[0].Nodes.Add(group1.Key);
var lv2Group = group1.ToList().GroupBy(x => x.CategoryName);
i++;
int j = -1;
foreach (IGrouping<string,ElementImpression> group2 in lv2Group)
{
treeView1.Nodes[0].Nodes[i].Nodes.Add(group2.Key);
var lv3Group = group2.ToList();
j++;
int k = -1;
foreach (ElementImpression ei in lv3Group)
{
k++;
treeView1.Nodes[0].Nodes[i].Nodes[j].Nodes.Add(ei.FamilyAndTypeName);
treeView1.Nodes[0].Nodes[i].Nodes[j].Nodes[k].Nodes.Add(ei.ElementId.ToString());
treeView1.Nodes[0].Nodes[i].Nodes[j].Nodes[k].Nodes.Add(ei.CategoryNumber.ToString());
}
}
}
treeView1.EndUpdate();
}
Is it possible to rewrite the UpdateTreeView() method, so that it accepts some kind of object, which tells the method how many levels and what properties to use, and then parses the data and creates the treeview dynamically at runtime? Is it possible to do it using recursion?