Child and parents

4 posts, 2 answers
  1. Svein Thomas
    Svein Thomas avatar
    47 posts
    Member since:
    Nov 2010

    Posted 28 Apr 2011 Link to this post

    Hi,

    Just downloaded the new 2011, Q1 components to see if the new treeview could do the magic i want it to do, but it seems like it couldn't OR?

    here is my code.. could someone guide me in the correct direction. 

    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;
     
    namespace RadControlsWinFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                setup();
            }
            public void setup()
            {
                KOMP_G komp_g1 = new KOMP_G() { KOMP_G_ID = 1, NAVN = "Komp1", KOMP_UG = new List<KOMP_UG>() };
                KOMP_G komp_g2 = new KOMP_G() { KOMP_G_ID = 2, NAVN = "Komp2", KOMP_UG = new List<KOMP_UG>() };
     
                KOMP_UG komp_ug1 = new KOMP_UG() { KOMP_UG_ID = 1, NAVN = "Komp_g1 -> komp_ug1", KOMPs = new List<KOMP>() };
                KOMP_UG komp_ug2 = new KOMP_UG() { KOMP_UG_ID = 2, NAVN = "Komp_g1 -> komp_ug2", KOMPs = new List<KOMP>() };
                KOMP_UG komp_ug3 = new KOMP_UG() { KOMP_UG_ID = 3, NAVN = "Komp_g1 -> komp_ug3", KOMPs = new List<KOMP>() };
                KOMP_UG komp_ug4 = new KOMP_UG() { KOMP_UG_ID = 4, NAVN = "Komp_g1 -> komp_ug4", KOMPs = new List<KOMP>() };
     
                KOMP komp1 = new KOMP() { KOMP_ID = 1, NAVN = "A" };
                KOMP komp2 = new KOMP() { KOMP_ID = 2, NAVN = "B" };
                KOMP komp3 = new KOMP() { KOMP_ID = 3, NAVN = "C" };
                KOMP komp4 = new KOMP() { KOMP_ID = 4, NAVN = "D" };
                KOMP komp5 = new KOMP() { KOMP_ID = 5, NAVN = "E" };
                KOMP komp6 = new KOMP() { KOMP_ID = 6, NAVN = "F" };
     
                komp_ug1.KOMPs.Add(komp1);
                komp_ug1.KOMPs.Add(komp2);
                komp_ug2.KOMPs.Add(komp3);
                komp_ug3.KOMPs.Add(komp4);
                komp_ug4.KOMPs.Add(komp5);
                komp_ug4.KOMPs.Add(komp6);
     
                komp_g1.KOMP_UG.Add(komp_ug1);
                komp_g1.KOMP_UG.Add(komp_ug2);
                komp_g2.KOMP_UG.Add(komp_ug3);
                komp_g2.KOMP_UG.Add(komp_ug4);
     
     
     
                List<IKomp> komps = new List<IKomp>();
                komps.Add(komp_g1);
                komps.Add(komp_g2);
     
     
                //SETUP TreeView
                radTreeView1.DataSource = komps;
                radTreeView1.DisplayMember = "Description";
                radTreeView1.ValueMember = "Value";
                radTreeView1.ParentMember = "Parent";
                radTreeView1.ChildMember = "Children";
            }
     
        }
     
     
        public interface IKomp
        {
            string Value { get; }
            string Description { get; }
            IKomp Parent { get; }
            IEnumerable<IKomp> Children { get; }
        }
     
        public  class KOMP : IKomp
        {
     
            public int KOMP_ID;
            public string NAVN;
            public KOMP_UG KOMP_UG;
     
            #region IKomp Members
     
            public string Value
            {
                get { return this.KOMP_ID.ToString(); }
            }
     
            public string Description
            {
                get { return this.NAVN; }
            }
            public IKomp Parent
            {
                get { return this.KOMP_UG; }
            }
            public IEnumerable<IKomp> Children
            {
                get { return null; }
            }
     
            #endregion
        }
     
        public  class KOMP_G : IKomp
        {
            public int KOMP_G_ID;
            public string NAVN;
            public List<KOMP_UG> KOMP_UG;
            #region IKomp Members
     
            public string Value
            {
                get { return this.KOMP_G_ID.ToString(); }
            }
     
            public string Description
            {
                get { return this.NAVN; }
            }
            public IKomp Parent
            {
                get { return null; }
            }
            public IEnumerable<IKomp> Children
            {
                get { return this.KOMP_UG.Cast<IKomp>(); }
            }
     
            #endregion
     
     
        }
         
        public class KOMP_UG : IKomp
        {
     
            public int KOMP_UG_ID;
            public string NAVN;
            public KOMP_G KOMP_G;
            public List<KOMP> KOMPs;
     
            #region IKomp Members
     
            public string Value
            {
                get { return this.KOMP_UG_ID.ToString(); }
            }
     
            public string Description
            {
                get { return this.NAVN; }
            }
            public IKomp Parent
            {
                get { return this.KOMP_G; }
            }
            public IEnumerable<IKomp> Children
            {
                get { return this.KOMPs.Cast<IKomp>(); }
            }
     
            #endregion
     
        }
    }
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Apr 2011 Link to this post

    Hello Svein,

    Just change the binding code a little, from this:
    //SETUP TreeView
    radTreeView1.DataSource = komps;
    radTreeView1.DisplayMember = "Description";
    radTreeView1.ValueMember = "Value";
    radTreeView1.ParentMember = "Parent";
    radTreeView1.ChildMember = "Children";
    to this:
    //SETUP TreeView
    radTreeView1.DataSource = komps;
    radTreeView1.ChildMember = @"KOMP_G\Children\Children";
    radTreeView1.DisplayMember = @"Description\Description\Description";

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Svein Thomas
    Svein Thomas avatar
    47 posts
    Member since:
    Nov 2010

    Posted 02 May 2011 Link to this post

    One detail that does not seem to work with this is setup is AutoCheckChildNodes and TriStateMode.
    How can i make this work?

    /ST
  5. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 02 May 2011 Link to this post

    Hello again Svein,

    Nice catch on this one, this only happens when you set TriStateMode = true  && CheckBoxes = true.

    Please use the following as a workaround, enable TriStateMode & disable CheckBoxes and use the following:
        radTreeView1.AutoCheckChildNodes = true;
        radTreeView1.TriStateMode = true;
        radTreeView1.CheckBoxes = false;
        ShowCheckBoxes(radTreeView1.Nodes);
     
    private void ShowCheckBoxes(RadTreeNodeCollection nodes)
    {
        foreach (var node in nodes)
        {
            node.CheckType = Telerik.WinControls.UI.CheckType.CheckBox;
            ShowCheckBoxes(node.Nodes);
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
Back to Top