The time in Italy is:
Sunday, 1 Aug 2021
You are here: HomeArticlesHow rebind RadOrgChart Telerik

How rebind RadOrgChart Telerik

RadOrgChart RadOrgChart

In this article you can find how resolve the problem about rebind data into the Telerik RadOrgChart control.

 I've used a programmatic data binding with DataTable and Sql Datasource but, when I've tried to filter the data, I've finded a problem with RadOrgChart .

If I've bound data in Load Page method the control not show the new data but continue to show the old data.

You can see a simple example here.

C# with DataTable

protected void Page_Load(object sender, EventArgs e)
{
	CreateOrg();
}
  
private void CreateOrg()
{
	DataTable nodeTable = CreateNode();
	DataTable itemsTable = CreateItems();
	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldID = "ID";
	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldParentID = "ParentID";
	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataSource = nodeTable;
	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldNodeID = "NodeID";
	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldID = "ID";
	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataSource = itemsTable;
	RadOrgChart.DataBind();
	RadOrgChart.CollapseAllNodes();
}
  
private DataTable CreateNode()
{
	DataTable nodeTable = new DataTable();
	nodeTable.Columns.Add("ID");
	nodeTable.Columns.Add("ParentID");
	nodeTable.Rows.Add(new String[] { "0", null });
	nodeTable.Rows.Add(new String[] { "1", "0" });
	nodeTable.Rows.Add(new String[] { "2", "0" });
	nodeTable.Rows.Add(new String[] { "3", "1" });
	nodeTable.Rows.Add(new String[] { "4", "2" });
	return nodeTable;
}
  
private DataTable CreateItems()
{
	DataTable itemsTable = new DataTable();
	itemsTable.Columns.Add("NodeID");
	itemsTable.Columns.Add("ID");
	itemsTable.Columns.Add("Text");
	itemsTable.Rows.Add(new String[] { "0", "0", "Text1" });
	itemsTable.Rows.Add(new String[] { "1", "1", "Text2" });
	itemsTable.Rows.Add(new String[] { "2", "2", "Text3" });
	itemsTable.Rows.Add(new String[] { "3", "3", "Text4" });
	itemsTable.Rows.Add(new String[] { "3", "4", "Text5" });
	itemsTable.Rows.Add(new String[] { "4", "5", "Text6" });
	itemsTable.Rows.Add(new String[] { "4", "6", "Text7" });
	itemsTable.Rows.Add(new String[] { "4", "7", "Text8" });
	return itemsTable;
}

 

C# with SqlDataSource

protected void Page_Load(object sender, EventArgs e)
{
	CreateOrg();
}
  
private void CreateOrg()
{
	DataTable nodeTable = CreateNode();
  	DataTable itemsTable = CreateItems();
  	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldID = "ID";
  	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldParentID = "ParentID";
  	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataSource = nodeTable;
 	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldNodeID = "NodeID";
  	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldID = "ID";
  	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataSource = itemsTable;
  	RadOrgChart.DataBind();
  	RadOrgChart.CollapseAllNodes();
}
  
private DataTable CreateNode()
{
	DataTable nodeTable = new DataTable();
	nodeTable.Columns.Add("ID");
	nodeTable.Columns.Add("ParentID");
	//query in SqlDataSourceGroupByNode
	//select * from tablename group by NodeID
  	DataView dv = (DataView)SqlDataSourceGroupByNode.Select(DataSourceSelectArguments.Empty);
  	foreach (DataRowView drv in dv)
	{
		nodeTable.Rows.Add(new String[] { drv["NodeID"].ToString(), drv["ParentID"].ToString() });
	}
	
	return nodeTable;
}
  
private DataTable CreateItems()
{
	DataTable itemsTable = new DataTable();
  
	itemsTable.Columns.Add("NodeID");
  	itemsTable.Columns.Add("ID");
  	itemsTable.Columns.Add("Title");
  	itemsTable.Columns.Add("ToolTip");
  	itemsTable.Columns.Add("MainValue");
  	itemsTable.Columns.Add("SecondaryValue");
	//query in SqlDataSourceNicolaSlobbe
	//select * from tablename  
  	DataView dv = (DataView)SqlDataSourceNicolaSlobbe.Select(DataSourceSelectArguments.Empty);
      
	foreach (DataRowView drv in dv)
	{
		itemsTable.Rows.Add(new String[] { drv["ID"].ToString(), drv["NodeID"].ToString(), drv["Title"].ToString(), drv["ToolTip"].ToString(), drv["MainValue"].ToString(), drv["SecondaryValue"].ToString() });	
	}
    
    return itemsTable;
}

 

Usually the contol is created into the Load Page method but for resolve the problem you need to use the prerender method of the RadOrgChart control.

You can see the solution below.

C# with DataTable

protected void RadOrgChart_PreRender(object sender, EventArgs e)
{
	CreateOrg();
}
  
private void CreateOrg()
{
	DataTable nodeTable = CreateNode();
	DataTable itemsTable = CreateItems();
	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldID = "ID";
	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldParentID = "ParentID";
	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataSource = nodeTable;
	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldNodeID = "NodeID";
	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldID = "ID";
	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataSource = itemsTable;
	RadOrgChart.DataBind();
	RadOrgChart.CollapseAllNodes();
}
  
private DataTable CreateNode()
{
	DataTable nodeTable = new DataTable();
	nodeTable.Columns.Add("ID");
	nodeTable.Columns.Add("ParentID");
	nodeTable.Rows.Add(new String[] { "0", null });
	nodeTable.Rows.Add(new String[] { "1", "0" });
	nodeTable.Rows.Add(new String[] { "2", "0" });
	nodeTable.Rows.Add(new String[] { "3", "1" });
	nodeTable.Rows.Add(new String[] { "4", "2" });
	return nodeTable;
}
  
private DataTable CreateItems()
{
	DataTable itemsTable = new DataTable();
	itemsTable.Columns.Add("NodeID");
	itemsTable.Columns.Add("ID");
	itemsTable.Columns.Add("Text");
	itemsTable.Rows.Add(new String[] { "0", "0", "Text1" });
	itemsTable.Rows.Add(new String[] { "1", "1", "Text2" });
	itemsTable.Rows.Add(new String[] { "2", "2", "Text3" });
	itemsTable.Rows.Add(new String[] { "3", "3", "Text4" });
	itemsTable.Rows.Add(new String[] { "3", "4", "Text5" });
	itemsTable.Rows.Add(new String[] { "4", "5", "Text6" });
	itemsTable.Rows.Add(new String[] { "4", "6", "Text7" });
	itemsTable.Rows.Add(new String[] { "4", "7", "Text8" });
	return itemsTable;
}

 

C# with SqlDataSource

protected void RadOrgChart_PreRender(object sender, EventArgs e)
{
	CreateOrg();
}
  
private void CreateOrg()
{
	DataTable nodeTable = CreateNode();
  	DataTable itemsTable = CreateItems();
  	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldID = "ID";
  	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataFieldParentID = "ParentID";
  	RadOrgChart.GroupEnabledBinding.NodeBindingSettings.DataSource = nodeTable;
 	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldNodeID = "NodeID";
  	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataFieldID = "ID";
  	RadOrgChart.GroupEnabledBinding.GroupItemBindingSettings.DataSource = itemsTable;
  	RadOrgChart.DataBind();
  	RadOrgChart.CollapseAllNodes();
}
  
private DataTable CreateNode()
{
	DataTable nodeTable = new DataTable();
	nodeTable.Columns.Add("ID");
	nodeTable.Columns.Add("ParentID");
	//query in SqlDataSourceGroupByNode
	//select * from tablename group by NodeID
  	DataView dv = (DataView)SqlDataSourceGroupByNode.Select(DataSourceSelectArguments.Empty);
  	foreach (DataRowView drv in dv)
	{
		nodeTable.Rows.Add(new String[] { drv["NodeID"].ToString(), drv["ParentID"].ToString() });
	}
	
	return nodeTable;
}
  
private DataTable CreateItems()
{
	DataTable itemsTable = new DataTable();
  
	itemsTable.Columns.Add("NodeID");
  	itemsTable.Columns.Add("ID");
  	itemsTable.Columns.Add("Title");
  	itemsTable.Columns.Add("ToolTip");
  	itemsTable.Columns.Add("MainValue");
  	itemsTable.Columns.Add("SecondaryValue");
	//query in SqlDataSourceNicolaSlobbe
	//select * from tablename  
  	DataView dv = (DataView)SqlDataSourceNicolaSlobbe.Select(DataSourceSelectArguments.Empty);
      
	foreach (DataRowView drv in dv)
	{
		itemsTable.Rows.Add(new String[] { drv["ID"].ToString(), drv["NodeID"].ToString(), drv["Title"].ToString(), drv["ToolTip"].ToString(), drv["MainValue"].ToString(), drv["SecondaryValue"].ToString() });	
	}
    
    return itemsTable;
}

 

Last modified on Tuesday, 01 September 2015 09:53

Additional Info

  • Technology: Visual Studio 2012, Telerik, C#

Comments  

0 # gay 2015-02-17 20:31
Appreciate it for helping out, great info. "The laws of probability, so true in general, so fallacious in particular." by Edward Gibbon.
Reply
0 # App DeveloperJess 2015-07-23 15:10
I noticed in the createNode class the rows are created manually. Can you do this with a dataset? And can the 2 variables be strings?
for each row in dataset.rows
nodeTable.Rows.Add(new String[] {row("employeenumber", row("reportsto")});
next

Sorry if the syntax is off usually do this in VB.
Reply
0 # C# with SqlDataSourceNicola Slobbe 2015-08-07 16:56
I added an example with SqlDataSource. It's not an optimized code but I'll hope this help you.
Reply

Add comment


Security code
Refresh

Go to top