Binding with BindingList

7 posts, 1 answers
  1. Patrick
    Patrick avatar
    3 posts
    Member since:
    Feb 2011

    Posted 08 Feb 2011 Link to this post

    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!!!
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Feb 2011 Link to this post

    Hello,

    I'll see if I can put something together for you. Do you prefer C# or VB?
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Patrick
    Patrick avatar
    3 posts
    Member since:
    Feb 2011

    Posted 08 Feb 2011 Link to this post

    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!
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Feb 2011 Link to this post

    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
  6. Patrick
    Patrick avatar
    3 posts
    Member since:
    Feb 2011

    Posted 08 Feb 2011 Link to this post

    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.
  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Feb 2011 Link to this post

    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
  8. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Feb 2011 Link to this post

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