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

TreeView will not update after DataSource has been set to null

1 Answer 210 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Randy
Top achievements
Rank 1
Randy asked on 15 Apr 2013, 04:56 PM
When using an object-relational binding with the TreeView control, if the DataSource is set to something, set to null and then set to something again the tree will no longer update.

Here is a code sample:

1. Click Load - the base data appears in the tree.
2. Click Add - a new node appear in the tree.
3. Click Clear - all nodes are erased.
4. Click Load - base data is added back in and appears
5. Click Add - nothing happens...

public partial class Form1 : Form
   {
       BindingList<Thing> RootChildren;
       int currentId = 1;
 
       public Form1()
       {
           InitializeComponent();
       }
 
       private void buttonLoad_Click(object sender, EventArgs e)
       {
           radTreeView1.DataSource = null;
           RootChildren = new BindingList<Thing>();
 
           Thing t1 = new Thing(1, "One");
           Thing t2 = new Thing(2, "Two");
           Thing t3 = new Thing(3, "Three");
           Thing t4 = new Thing(4, "Four");
           currentId = 5;
 
           RootChildren.Add(t1);
           RootChildren.Add(t2);
           RootChildren.Add(t3);
           RootChildren.Add(t4);
 
           radTreeView1.DataSource = RootChildren;
           radTreeView1.ChildMember = @"Children\Children\Children\Children";
           radTreeView1.ValueMember = "Id";
           radTreeView1.DisplayMember = "Name";
       }
 
       private void buttonClear_Click(object sender, EventArgs e)
       {
           radTreeView1.DataSource = null;
       }
 
       private void buttonAdd_Click(object sender, EventArgs e)
       {
           Thing thing = new Thing(currentId, "NewThing" + currentId);
           RootChildren.Add(thing);
           currentId++;
 
           RootChildren.ResetBindings();
       }
   }
 
   public class Thing
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public BindingList<Thing> Children = new BindingList<Thing>();
 
       public Thing() { }
       public Thing(int id, string name)
       {
           Id = id;
           Name = name;
       }
   }

1 Answer, 1 is accepted

Sort by
0
Accepted
Peter
Telerik team
answered on 18 Apr 2013, 11:26 AM
Hi Randy,

Thank you for the attached project.

I was able to reproduce the described behavior. You should to reset the RadTreeView's ChildMember property when you reset the DataSource property:
radTreeView1.DataSource = null;
radTreeView1.ChildMember = "";

This will allow RadTreeView to create and associate new relation objects for the new data source.

I hope this helps.

All the best,
Peter
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Tags
Treeview
Asked by
Randy
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or