TreeView will not update after DataSource has been set to null

2 posts, 1 answers
  1. Randy
    Randy avatar
    19 posts
    Member since:
    Oct 2012

    Posted 15 Apr 2013 Link to this post

    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;
           }
       }
  2. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 18 Apr 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top