I have a set of child nodes under a parent node that I sort by a "Sort Order" column in the database based on importance.
However, when I bind, the child nodes insist on sorting alphabetically. How can I force the treeview to accept the list order as is or is there a way to sort after I bind it?
Thanks, Dave
3 Answers, 1 is accepted
Thank you for contacting Telerik Support.
An easy and convenient way of achieving such functionality would be to use the "order by" clause in your sql query. This way the result retrieved by the query will be sorted and then you can use it as a RadTreeView data source. Please refer to this help article for more information.
Also please find in this knowledge base article an approach using server-side code for sorting RadTreeView's Nodes by Text/Value.
Hope that this will be helpful.
Regards,
Boyan Dimitrov
Telerik

That's just it. I'm sorting by a "SortOrder" column on the backend as you suggest.
My collection is listed in this order in the debugger (* indicates values replaced for privacy). "All" being the parent node and the rested as nested nodes one level down. This is how it looks during when I call DataBind.
items[0].Text
"All"
items[1].Text
"O******A"
items[2].Text
"O******R"
items[3].Text
"OR*****A"
items[4].Text
"S******R"
items[5].Text
"P******R"
items[6].Text
"P******C"
When I bind the items collection below it insists on sorting the .Text alpha.
RadTreeViewSearchItems.DataTextField = "Text";
RadTreeViewSearchItems.DataFieldID = "RowId";
RadTreeViewSearchItems.DataValueField = "Id";
RadTreeViewSearchItems.DataFieldParentID = "ParentRowId";
RadTreeViewSearchItems.DataSource = items;
RadTreeViewSearchItems.DataBind();
The treeview is defined as follows:
<telerik:RadTreeView
ID="RadTreeViewSearchItems"
OnNodeDataBound="RadTreeViewSearchItems_NodeDataBound"
OnNodeCreated="RadTreeViewSearchItems_NodeCreated"
runat="server"
CheckBoxes="true"
CheckChildNodes="true"
ShowLineImages="false"
TriStateCheckBoxes="false"
Height="100%"
Width="100%"
style="overflow-x:hidden;
overflow-y:scroll;"
data-control-type="radtreeview">
<DataBindings>
<telerik:RadTreeNodeBinding Expanded="true">
</telerik:RadTreeNodeBinding>
</DataBindings>
<NodeTemplate>
<%# DataBinder.Eval(Container, "Text") %>
</NodeTemplate>
</telerik:RadTreeView>
Please find attached a sample project that demonstrates binding the RadTreeView control to a data set retrieved from a data table. For testing purposes I have commented the default sql query ( without using the order clause by specific column). You can test the application with both queries and notice the differences. When the "ORDER BY LastName" is used all employees are listed in alphabetical order.
//code behind
private
static
void
BindToDataSet(RadTreeView treeView)
{
SqlDataAdapter adapter =
new
SqlDataAdapter(
"SELECT * FROM Employees ORDER BY LastName ASC"
,
ConfigurationManager.ConnectionStrings[
"NorthwindConnectionString"
].ConnectionString);
//SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees",
// ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
DataSet links =
new
DataSet();
adapter.Fill(links);
treeView.DataTextField =
"LastName"
;
treeView.DataFieldID =
"EmployeeID"
;
treeView.DataFieldParentID =
"ReportsTo"
;
treeView.DataSource = links;
treeView.DataBind();
}
Regards,
Boyan Dimitrov
Telerik