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

How to sort child nodes or keep initial sort order?

3 Answers 608 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 10 Jan 2014, 06:44 PM
Hi,

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

Sort by
0
Boyan Dimitrov
Telerik team
answered on 15 Jan 2014, 08:55 AM
Hello Dave,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
David
Top achievements
Rank 1
answered on 15 Jan 2014, 03:16 PM
Boyan,

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>

 

 

 

 


0
Boyan Dimitrov
Telerik team
answered on 20 Jan 2014, 02:04 PM
Hello,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
TreeView
Asked by
David
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
David
Top achievements
Rank 1
Share this question
or