This is a migrated thread and some comments may be shown as answers.

Treeview not showing child nodes added to data bound business object

1 Answer 466 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Erica
Top achievements
Rank 1
Erica asked on 21 Aug 2013, 10:35 PM
I'm using 2013 Q2. I have a Treeview bound to a basic custom business object. My data source is of type BindingList<Company>. Each company has a BindingList of projects, and each Project has a BindingList of Phases:

class Company : INotifyPropertyChanged
{
    private string _code;
    private readonly  BindingList<Project> _projects;
 
    public Company(string companyCode):this(companyCode,null){}
 
    public Company(string companyCode, IList<Project> projects)
    {
        _code = companyCode;
        _projects = projects == null ? new BindingList<Project>() : new BindingList<Project>(projects);
    }
 
    public String Code
    {
        get { return _code; }
        set
        {
            _code = value;
            OnPropertyChanged("Code");
        }
    }
 
    public BindingList<Project> Projects
    {
        get { return _projects; }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
   
 
}

class Project : INotifyPropertyChanged
{
    private string _code;
    private readonly BindingList<Phase> _phases;
 
    public Project(string projectCode) : this(projectCode, null) { }
 
    public Project(string projectCode, IList<Phase> phases)
    {
        _code = projectCode;
        _phases = phases == null ? new BindingList<Phase>() : new BindingList<Phase>(phases);
 
    }
 
    public String Code
    {
        get { return _code; }
        set
        {
            _code = value;
            OnPropertyChanged("Code");
        }
    }
 
    public BindingList<Phase> Phases
    {
        get { return _phases; }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
 
 
}

I've omitted the code for the Phase class, but it is similar.

I bound the data object like this:

_companiesTreeView.DataSource = _dataSource;
_companiesTreeView.DisplayMember = "Code\\Code\\Code";
_companiesTreeView.ChildMember = "Companies\\Projects\\Phases";
_companiesTreeView.ValueMember = "Code\\Code\\Code";

This mostly works as expected. Changes to my data objects at all levels are reflected in the tree. When I add a top level node (Company), it is also immediately reflected in the tree. However, when I add a child node (Project) to a company, the child node is not displayed unless I set the BindingSource to null and then back to my data source. I could do this as a workaround, but that would also require a lot of fiddling around to restore the state of all the nodes in the tree.

Is it possible for the binding to work as I would expect and display newly added child nodes in the tree?

ETA: Version number

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 26 Aug 2013, 12:51 PM
Hello Erica,

Thank you for writing.

This is the default behaviour of the tree view and it reflects changes only for its first level. It is designed like this, because it can have a lot of hierarchy levels with a lot of nodes and this will cost a lot of resources to manage. In your case you can reset the treeview like this when you need to reload the data from the source:
radTreeView1.TreeViewElement.TreeNodeProvider.Reset();

Regardless of how you are rebinding the treeview you will have to manually restore its previous states - expanded nodes, scroll position, etc.

I hope this will be useful. Should you have further questions, I would be glad to help.

Regards,
Dimitar
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
Treeview
Asked by
Erica
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or