How to sort child nodes or keep initial sort order?

4 posts, 0 answers
  1. David
    David avatar
    48 posts
    Member since:
    Nov 2012

    Posted 10 Jan 2014 Link to this post

    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
  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 15 Jan 2014 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David
    David avatar
    48 posts
    Member since:
    Nov 2012

    Posted 15 Jan 2014 Link to this post

    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>

     

     

     

     


  5. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 20 Jan 2014 Link to this post

    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.
Back to Top