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

Binding with BindingList

6 Answers 248 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 1
Patrick asked on 08 Feb 2011, 02:44 PM
Does anyone have an example of how to bind a TreeView to a BindingList or preferably if possible to a BindingSource? I have tried both, and just cannot seem to get it to work right. Thanks in advanced!!!

6 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 04:01 PM
Hello,

I'll see if I can put something together for you. Do you prefer C# or VB?
Richard
0
Patrick
Top achievements
Rank 1
answered on 08 Feb 2011, 04:32 PM
Richard, I would appreciate it very much, I have spent multiple hours trying to figure out the problem I am having and having no luck, C# would be preferred. I can bind the TreeView to the BindingList but when I make updates to the nodes parents/text the Treeview does not update. 

Thanks again!
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 05:09 PM
Hi Patrick,

Firstly, have a look at this forum link as I think it may answer your question. If this does, then the new RadTreeView will be released as part of Q1 2011 which is due out mid-March.

Let me know if that helps
Richard
0
Patrick
Top achievements
Rank 1
answered on 08 Feb 2011, 06:43 PM
Richard, I did take a look at that forum post the other day, and I am doing what is written on that post as far as databinding the TreeView to a BindingList, the TreeView is loaded with the correct data, but my problem comes in when I want to update a particular node. If I want to change the parent of a particular node, the treeview does not automatically update. What I am doing for now as a work-around is setting the datasource of the treeview to NULL then rebinding the control to the BindingList. I will try to create a sample project that demostrates this behavior this evening.
0
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 08:03 PM
Hello Patrick,

Yes, to my knowledge this is what you will need to do for the moment until the Q1 2011 release is ready. If I can do anything to help in the meantime though, do let me know
Richard
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 08 Feb 2011, 08:21 PM
Hi Patrick,

BindingSource.ResetBindings() also has the desired effect.

Designer File
namespace RadTreeView_C
partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radButton1 = new Telerik.WinControls.UI.RadButton();
            this.radTreeView1 = new Telerik.WinControls.UI.RadTreeView();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radTreeView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radButton1
            // 
            this.radButton1.Location = new System.Drawing.Point(244, 276);
            this.radButton1.Name = "radButton1";
            this.radButton1.Size = new System.Drawing.Size(130, 24);
            this.radButton1.TabIndex = 1;
            this.radButton1.Text = "radButton1";
            this.radButton1.Click += new System.EventHandler(this.radButton1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.DarkGray;
            this.ClientSize = new System.Drawing.Size(386, 319);
            // 
            // radTreeView1
            // 
            this.radTreeView1.BackColor = System.Drawing.SystemColors.Window;
            this.radTreeView1.Cursor = System.Windows.Forms.Cursors.Default;
            this.radTreeView1.Font = new System.Drawing.Font("Segoe UI", 8.25F);
            this.radTreeView1.ForeColor = System.Drawing.Color.Black;
            this.radTreeView1.Location = new System.Drawing.Point(12, 12);
            this.radTreeView1.Name = "radTreeView1";
            this.radTreeView1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            // 
            // 
            // 
            this.radTreeView1.RootElement.ForeColor = System.Drawing.Color.Black;
            this.radTreeView1.Size = new System.Drawing.Size(362, 250);
            this.radTreeView1.TabIndex = 0;
            this.radTreeView1.Text = "radTreeView1";
            this.Controls.Add(this.radButton1);
            this.Controls.Add(this.radTreeView1);
            this.MinimizeBox = false;
            this.Name = "Form1";
            // 
            // 
            // 
            this.RootElement.ApplyShapeToControl = true;
            this.Text = "Form1";
            this.ThemeName = "Windows7";
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radTreeView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadTreeView radTreeView1;
        private Telerik.WinControls.UI.RadButton radButton1;
  
  
  
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.Collections.ObjectModel;
  
using System.Runtime.InteropServices;
  
namespace RadTreeView_C
{
    public partial class Form1 : RadForm
    {
        BindingList<Person> people;
  
  
        public Form1()
        {
            InitializeComponent();
  
            this.people = new BindingList<Person>();
            this.people.Add(new Person(1, 0, "Peter"));
            this.people.Add(new Person(2, 0, "Stefan"));
            this.people.Add(new Person(3, 1, "John"));
            this.people.Add(new Person(4, 2, "James"));
            this.people.Add(new Person(5, 4, "Ivan"));
  
            this.radTreeView1.DataSource = this.people;
            this.radTreeView1.ValueMember = "Id";
            this.radTreeView1.DisplayMember = "Name";
            this.radTreeView1.ParentIDMember = "ParentId";
  
            this.radTreeView1.AllowEdit = true;
            this.radTreeView1.AllowDefaultContextMenu = true;
  
            this.radTreeView1.ExpandAll();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
        }
  
        private void radButton1_Click(object sender, EventArgs e)
        {
            Person john = (Person)this.radTreeView1.Nodes[0].Nodes[0].DataBoundItem;
            john.ParentId = 2; // move john to under stefan
            people.ResetBindings();
            this.radTreeView1.ExpandAll();
        }
  
    }
  
    public class Person
    {
        string name;
        int id;
        int parentId;
  
        public Person()
        {
  
        }
  
        public Person(int id, int parentId, string name)
        {
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }
  
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
  
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
  
        public int ParentId
        {
            get { return parentId; }
            set { parentId = value; }
        }
    }
  
  
  
   
  
  
}

Hope that helps
Richard
Tags
Treeview
Asked by
Patrick
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Patrick
Top achievements
Rank 1
Share this question
or