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

Child and parents

3 Answers 173 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Svein Thomas
Top achievements
Rank 1
Svein Thomas asked on 28 Apr 2011, 02:35 PM
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
 
    }
}

3 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 04:47 PM
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
0
Svein Thomas
Top achievements
Rank 1
answered on 02 May 2011, 11:42 AM
One detail that does not seem to work with this is setup is AutoCheckChildNodes and TriStateMode.
How can i make this work?

/ST
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 02 May 2011, 09:09 PM
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
Tags
Treeview
Asked by
Svein Thomas
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Svein Thomas
Top achievements
Rank 1
Share this question
or