Something I’m doing very often in my winforms is binding a list of objects (with hierarchy) in my radgridview.
because our hierarchy structure is based on left and right (more information about this here).
Okay lets start at the beginning. for this example I’m keeping it simple actually, it’s just that you get the idea.
First thing you have to do is creating your list of objects with all the data you need (the hierachical structure also).
In this example I’m creating a simple object for the moment.
//load your List
var myListMaster = new List();
var child1 = new List();
child1.Add(new Child{ Id = 2, Name = "ChildTest2" });
child1.Add(new Child{ Id = 3, Name = "ChildTest3" });
var child2 = new List();
child1.Add(new Child{ Id = 5, Name = "ChildTest5" });
child1.Add(new Child{ Id = 6, Name = "ChildTest6" });
child1.Add(new Child{ Id = 7, Name = "ChildTest7" });
myListMaster.Add(new Master { Id = 1, Name = "Test", Childs = child1 });
myListMaster.Add(new Master { Id = 4, Name = "Test2", Childs = child2 });
Once you have your data into your objects you start to create your columns and add your data.
1 //clear the gridview of everything
2 radGridView.MasterGridViewTemplate.Columns.Clear();
3 radGridView.MasterGridViewTemplate.Rows.Clear();
4 radGridView.MasterGridViewTemplate.ChildGridViewTemplates.Clear();
5
6
7 //add the columns for the master
8 radGridView.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Id", HeaderText = "Name", HeaderTextAlignment = ContentAlignment.MiddleCenter });
9 radGridView.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Name", HeaderText = "Adserver Id", HeaderTextAlignment = ContentAlignment.MiddleCenter });
10 radGridView.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Active", HeaderText = "Active", HeaderTextAlignment = ContentAlignment.MiddleCenter });
11
12 //add the columns for the childs
13 var template = new GridViewTemplate { AllowAddNewRow = false, AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill };
14 template.Columns.Add(new GridViewTextBoxColumn { FieldName = "Id", IsVisible = false });
15 template.Columns.Add(new GridViewTextBoxColumn { FieldName = "IdChild", HeaderText = "Zone Name", HeaderTextAlignment = ContentAlignment.MiddleCenter});
16 template.Columns.Add(new GridViewTextBoxColumn { FieldName = "NameChild", HeaderText = "Zone Id", HeaderTextAlignment = ContentAlignment.MiddleCenter});
17
18 //add the childtemplate to the master
19 radGridView.MasterGridViewTemplate.ChildGridViewTemplates.Add(template);
20
21 //make the relationship between master and child
22 var relation = new GridViewRelation(radGridView.MasterGridViewTemplate)
23 {
24 ChildTemplate = template,
25 RelationName = "SiteZonesNetwork"
26 };
27
28 relation.ParentColumnNames.Add("Id"); //equals the fieldname
29 relation.ChildColumnNames.Add("Id"); //equals the fieldname
30
31 //add the relation
32 radGridView.Relations.Add(relation);
33
34
35 //load your List
36 var myListMaster = new List();
37
38 var child1 = new List();
39 child1.Add(new Child{ Id = 2, Name = "ChildTest2" });
40 child1.Add(new Child{ Id = 3, Name = "ChildTest3" });
41
42 var child2 = new List();
43 child1.Add(new Child{ Id = 5, Name = "ChildTest5" });
44 child1.Add(new Child{ Id = 6, Name = "ChildTest6" });
45 child1.Add(new Child{ Id = 7, Name = "ChildTest7" });
46
47 myListMaster.Add(new Master { Id = 1, Name = "Test", Childs = child1 });
48 myListMaster.Add(new Master { Id = 4, Name = "Test2", Childs = child2 });
49
50
51 //fill the radgridview
52 foreach(var myMaster in myListMaster){
53
54 radGridView.MasterGridViewTemplate.Rows.Add(new object[] { myMaster.Id, myMaster.Name, myMaster.Active });
55 template = radGridView.MasterGridViewTemplate.ChildGridViewTemplates[0];
56
57 foreach(var child in myMaster.childs){
58
59 template.Rows.Add(new object[] { myMaster.Id, child.Id, child.Name });
60 }
61
62 }
Where my 2 objects are written like this:
1 public class Master{
2 public int Id {get;set;}
3 public string Name {get;set;}
4 public List Childs{get;set;}
5 }
6
7 public class Child{
8 public int Id{get;set;}
9 public string Name {get;set;}
10 }
okay but what if your hierarchy is in one list of objects? So you have this structure:
class myObject{
public int Id {get;set;}
public string Name {get;set;}
public int ParentId {get;set;}
}
the thing is that you have a list of myObject with both parent and child.
now the only part of the above code you have to change is the reading of the object
1 foreach (var item in myObjectList )
2 {
3 if (item.ParentId == 0) //we've got a parent
4 {
5 gridview.MasterGridViewTemplate.Rows.Add(new object[] { item.Id, item.Name });
6 template = gridview.MasterGridViewTemplate.ChildGridViewTemplates[0];
7 }
8 else // we've got or child
9 {
10 if (template == null) continue;
11 template.Rows.Add(new object[] { item.ParentId, item.Id, item.Name});
12 }
13 }
This is only an example of a 2 level hierarchy. You can easily add more levels to your hierarchy.
I hope this helps for some people
Greetzz and until next post