Search code examples
winformsdata-bindingteleriktreeviewtreenode

WinForm TreeView nodes are empty despite having a DataSource to object-relational list


In my Telerik WinForms application, I'm able to bind a TreeView to a Well object in my application through object-relational binding as such:

this.wellTreeView = new RadTreeView();

BindingList<Well> wells = new BindingList<Well>() { well };

this.wellTreeView.DataSource = wells;
this.wellTreeView.DisplayMember = "name\\name\\name";
this.wellTreeView.ChildMember = "wells\\layers\\items";

This binding works and I am able to view the TreeView hierarchy and select the nodes in the form. However, when trying to get the nodes in code, thewellTreeView is empty (0 nodes) and throws an Index out of range error in:

// this line should get the root node, i.e. the Well at top level
RadTreeNode node = wellTreeView.Nodes[0];

My Well class is defined like so:

public class Well
{
    public string name {get; set;}
    public List<Layer> layers {get; set;}
}
public class Layer
{
    public string name {get; set;};
    public List<Item> items {get; set;}
}
public class Item
{
    public string name {get; set;};
    public decimal length {get; set;}
}

I don't understand why the wellTreeView is empty without any nodes despite being able to view the tree in the form.

I've posted this on the Telerik forum too

Thank you so much, any help would be greatly appreciated!


Solution

  • The Telerik team also provided a solution that requires me to initialise BindingContext when building the controls programmatically:

    this.wellTreeView = new RadTreeView();
    this.wellTreeView.BindingContext = new BindingContext();
    

    Telerik forum