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

Hierarchical Binding with lists

8 Answers 272 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
helloS
Top achievements
Rank 1
helloS asked on 26 Jan 2009, 04:44 PM

Hello

I am attempting to bind 3 tables of data to a Treeview using lists:

 

 

 

 

 
            vwMarketTypex mtype = markettypecontroller.GetView(STR_MarketType, STR_TimePeriod);  
            List<Symbol> currencies = Symbolcontroller.GetSymbolsByMarketType(mtype.MarketTypeID);  
 
            List<Scenario> scenarios = senariocontroller.GetAllScenarios();  
            List<RZEROMarketData.Data.RuleSet> rulesetsx = rscontroller.GetAllRulesets();  
 
            radTreeView1.RootRelationDisplayName = "Curreny Pair";  
            
            var newRelationBinding = new RelationBinding("Scenarios", scenarios, null"ScenarioDescription""ScenarioId");  
     
            radTreeView1.RelationBindings.Add(newRelationBinding);  
      
            var newRelationBinding1 = new RelationBinding("Rulesets", rulesetsx, null"Name""RulesetId");  
            radTreeView1.RelationBindings.Add(newRelationBinding1);  
         
 
            radTreeView1.DataSource = currencies; 



The last object rulesetsx does not bind ..please can you advise if I am on the right track.

Thanks

 

 

 

 

 

8 Answers, 1 is accepted

Sort by
0
helloS
Top achievements
Rank 1
answered on 26 Jan 2009, 10:19 PM

 

 

 Hello
Resorted to binding the treeviewvia it's nodes
That way I have a handle on exactly what is happening


private void LoadTreeView()  
        {  
            vwMarketTypex mtype = markettypecontroller.GetView(STR_MarketType, STR_TimePeriod);  
            List<Symbol> currencies = Symbolcontroller.GetSymbolsByMarketType(mtype.MarketTypeID);  
 
 
 
            foreach (Symbol symbol in currencies)  
            {  
                var currnode = new RadTreeNode(symbol.Symbol1);  
                currnode.Tag = symbol.SymbolId;  
                currnode.ContextMenu = radContextMenu1;  
                radTreeView1.Nodes.Add(currnode);  
                List<Scenario> scenarios = senariocontroller.GetByCurrencyID(symbol.SymbolId);  
                foreach (Scenario scenario in scenarios)  
                {  
                    var scenarionode = new RadTreeNode(scenario.ScenarioDescription);  
                    scenarioscenarionode.Tag = scenario.ScenarioId;  
                    scenarionode.ContextMenu = radContextMenu1;  
                    currnode.Nodes.Add(scenarionode);  
 
                    List<RZEROMarketData.Data.RuleSet> rulesetsx =  
                        rscontroller.GetRulesetsByScenario(scenario.ScenarioId);  
                    foreach (RZEROMarketData.Data.RuleSet ruleSet in rulesetsx)  
                    {  
                        var rulesetonode = new RadTreeNode(ruleSet.Name);  
                        rulesetonode.Tag = ruleSet.RulesetId;  
                        rulesetonode.ContextMenu = radContextMenu1;  
                        scenarionode.Nodes.Add(rulesetonode);  
                    }  
                }  
            }  
        } 

 

0
Jordan
Telerik team
answered on 30 Jan 2009, 08:18 AM
Hi helloS,

I am glad to see that you have found a solution. Indeed if you need more control over the process of binding the RadTreeView to the data you have to implement that with custom logic (as you did).

Now, if you intend to use the data binding functionality of RadTreeView you should note that there is a difference when binding to a DataSet or to business objects. That difference comes from the presence of relations between the data table in a data set. When working with business objects, these relations are in most cases implemented with collection properties like in the example bellow.

So in order to bind RadTreeView to a list of business objects you could write the following code:
BindingList<Artist> artists = SampleRelatedData.GetArtistObjects(DataBindingTests.musicCollectionDataSet.Artists); 
 
RelationBinding albumsBinding = new RelationBinding("Albums", artists, "Albums"); 
albumsBinding.DisplayName = "Albums"
albumsBinding.ValueMember = "ID"
this.radTreeView1.RelationBindings.Add(albumsBinding); 
this.radTreeView1.RelationBindings.Add(new RelationBinding("Songs", artists, "Albums.Songs")); 
 
this.radTreeView1.RootRelationDisplayName = "Artists"
this.radTreeView1.ValueMember = "ID"
this.radTreeView1.DataSource = artists; 


And here is the Artist class with the Albums collection property:
public class Artist : DataObject 
    { 
        public Artist() 
        { 
        } 
 
        public Artist(string name) 
        { 
            this.name = name; 
        } 
 
        private string name; 
 
        public string Name 
        { 
            get 
            { 
                return this.name; 
            } 
            set 
            { 
                this.name = value; 
            } 
        } 
 
        private object id; 
 
        public object ID 
        { 
            get 
            { 
                return this.id; 
            } 
            set 
            { 
                this.id = value; 
            } 
        } 
 
        private BindingList<Album> albums = new BindingList<Album>(); 
 
        public BindingList<Album> Albums 
        { 
            get 
            { 
                return this.albums; 
            } 
        } 
 
        public override IList List 
        { 
            get 
            { 
                return this.albums; 
            } 
        } 
    } 
The Album and Song classes can be easily implemented in a similar manner.
I hope this helps.

Regards,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Buggagos
Top achievements
Rank 1
answered on 14 Jul 2010, 07:26 PM
Dear all,

i am new to Telerik and i am pulling hair (whatever is left) trying to figure out how to use the relations so as to have a 4 level deep hierarchy rendered on a treeview control.

Although what HelloS did makes sense, i think the whole point (or at least a good one) of using (and buying Telerik.Rad*) is to exactly avoid manually iterating the collections and iteratively build the treeview.

I have seen the examples and unfortunately they only do the easy job of showing how to render a 2 level deep relationship.

I would much appreciate any feedback on this and I think many others would benefit from a clear solution.

Here is a sample scenario to work on.

The world has countries. Each country has cities. Each city has municipalities. Each municipality has citizens.

Programmatically to get all the citizens of a country you would have to do:

myworld.countries("UK").cities("London").municipalities("central london").citizens

Rendering all 4 levels to a treeview based on the manual process of HelloS is easy. How about doing it the "relations" way (using objects)?

Many thanks!



0
Buggagos
Top achievements
Rank 1
answered on 14 Jul 2010, 07:56 PM
Damn, i think i got it worked out...It seems that even by describing one's problem helps !....

For anyone interested here is the solution based on the sample senario above:

Imports Telerik.WinControls
 
With myWorldDisplayForm
   .RadTreeView1.RootRelationDisplayName = "Earth"
   Dim newBinding As UI.RelationBinding
   newBinding = New UI.RelationBinding("Cities", myworld, "Countries.Cities", "CityName", "CityName")
   .RadTreeView1.RelationBindings.Add(newBinding)
   newBinding = New UI.RelationBinding("Municipalities", myworld, "Countries.Cities.Municipalities", "MunName", "MunName")
   .RadTreeView1.RelationBindings.Add(newBinding)
   newBinding = New UI.RelationBinding("Citizens", myworld, "Countries.Cities.Municipalities.Citizens", "PersonName", "PersonName")
   .RadTreeView1.RelationBindings.Add(newBinding)
   .RadTreeView1.DataSource = myworld.Countries
End With

Each of the corresponding classes has got some properties (anything you like) and a List of the children objects....For example the country object has got a List(Of Cities).

Hope this one clarifies a few things for new users of the controls like me....

Many thanks.
0
Victor
Telerik team
answered on 20 Jul 2010, 10:19 AM
Hello Buggagos,

Thank you for sharing your code. You can also take a look at the help article that describes how to use the RelationBinding objects. Please write again if you need further assistance with our controls.

Sincerely yours,
Victor
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 12 Feb 2011, 06:58 AM
I am attempting the hierarchial binding with lists and am getting an error that says "Child list for field _branches cannot be created."  My code is as follows..


RelationBinding branchesBinding = new RelationBinding("Branches", _branches);
                branchesBinding.DisplayName = "Name";
                branchesBinding.ValueMember = "OfficeBranchID";
                radTreeView.RelationBindings.Add(branchesBinding);
  
                radTreeView.RelationBindings.Add(new RelationBinding("Levels", _branches, "_branches.Levels"));
                radTreeView.RelationBindings.Add(new RelationBinding("Offices", _branches, "_branches.Levels.Offices"));
                radTreeView.RelationBindings.Add(new RelationBinding("Districts", _branches, "_branches.Levels.Offices.Districts"));
                radTreeView.RelationBindings.Add(new RelationBinding("Candidates", _branches, "_branches.Levels.Offices.Districts.Candidates"));
  
                radTreeView.RootRelationDisplayName = "Branches";
                radTreeView.ValueMember = "OfficeBranchID";
                radTreeView.DataSource = _branches;

My object is a List<BranchDO> _branches and each BranchDO contains a List<LevelDO> Levels and each LevelDO contains a List<OfficeDO> Offices and each OfficeDO contains a List<DistrictDO> Districts.  The child lists could contain data or could be null (or an empty list if that works better).  What am i missing?
0
Julian Benkov
Telerik team
answered on 17 Feb 2011, 09:23 AM
Hi john,

This functionality is not supported by the current version of RadTreeView. You can use RelationBinding only with DataSet / DataTable types of data with defined relation between tables. More information you can find in our online documentation. For your scenario you can try the Load-On-Demand feature of RadTreeView. 

I hope this was helpful.

Best wishes,
Julian Benkov
the Telerik team
0
Stefan
Telerik team
answered on 22 Mar 2011, 03:11 PM

Hello guys,

Please note that in Q1 2011 we have introduced a major upgrade of RAdTreeView control, which is now virtualized and fully customizable. Feel free to download the latest release and try it out. For more information on our latest release refer to this blog post.

Greetings,
Stefan
the Telerik team
Tags
Treeview
Asked by
helloS
Top achievements
Rank 1
Answers by
helloS
Top achievements
Rank 1
Jordan
Telerik team
Buggagos
Top achievements
Rank 1
Victor
Telerik team
John
Top achievements
Rank 1
Julian Benkov
Telerik team
Stefan
Telerik team
Share this question
or