Hello Vidhya,
Thank you for writing.
In order to create a hierarchical view for two related data source objects (e.g DataTables connected via "Id" and "ParentId" columns), all you need to do is to set up a parent
GridViewTemplate and a child
GridViewTemplate. Then add the child
GridViewTemplate in the parent GridViewTemplate.
Templates collection. The last necessary thing is to create a
GridViewRelation specifying the parent/child template and add this relation in the RadGridView.
Relations collection. Our
Binding to Hierarchical Data Programmatically help article is useful about this topic. Here is a sample code snippet, demonstrating how to set up a 10 level hierarchy:
int
level = 10;
Random rand =
new
Random();
int
currentLevel = 0;
public
Form1()
{
InitializeComponent();
DataTable masterDt =
new
DataTable();
masterDt.Columns.Add(
"Id"
,
typeof
(
int
));
masterDt.Columns.Add(
"ParentId"
,
typeof
(
int
));
masterDt.Columns.Add(
"Title"
,
typeof
(
string
));
for
(
int
j = 0; j < 2; j++)
{
masterDt.Rows.Add(j, 0,
"Parent."
+ currentLevel +
"."
+ j);
}
this
.radGridView1.MasterTemplate.DataSource = masterDt;
this
.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
CreateInnerTemplate(
this
.radGridView1.MasterTemplate, currentLevel);
}
private
void
CreateInnerTemplate(GridViewTemplate parentTemplate,
int
currentLevel)
{
if
(currentLevel < level)
{
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"ParentId"
,
typeof
(
int
));
dt.Columns.Add(
"Title"
,
typeof
(
string
));
for
(
int
i = 0; i < 2; i++)
{
for
(
int
j = 0; j < 10; j++)
{
dt.Rows.Add(j, i,
"Child."
+ currentLevel +
"."
+ j);
}
}
GridViewTemplate firstChildtemplate =
new
GridViewTemplate();
firstChildtemplate.DataSource = dt;
firstChildtemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
parentTemplate.Templates.Add(firstChildtemplate);
GridViewRelation relation =
new
GridViewRelation(parentTemplate);
relation.ChildTemplate = firstChildtemplate;
relation.RelationName =
"MasterParent"
;
relation.ParentColumnNames.Add(
"Id"
);
relation.ChildColumnNames.Add(
"ParentId"
);
radGridView1.Relations.Add(relation);
CreateInnerTemplate(firstChildtemplate, ++currentLevel);
}
}
Note that this is just a sample which purpose is to demonstrate the approach and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best.
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time.
Watch the videos and start improving your app based on facts, not hunches.