Treeview Sorting Perfomance [Resolved]

Posted by Vuyiswamb under ASP.NET on 2/8/2010 | Views : 3539 | Status : [Member] [MVP] [Administrator] | Replies : 2
<p>Good Day All.

Today is one of my days. i have binded the treeview from a Database and it gets populated fine and am sorting it after this way

private void SortTree(TreeView tv)
{
if (tv.Nodes != null)
{
for (int i = 0; i &lt; tv.Nodes.Count; i++)
{
TreeNode node = tv.Nodes[i];
SortNode(node);
}
}
}


private void SortNode(TreeNode node)
{
if (node.ChildNodes != null &amp;&amp; node.ChildNodes.Count &gt; 1)
{
for (int i = 0; i &lt; node.ChildNodes.Count; i++)
{
TreeNode childNode = node.ChildNodes[i];
SortNode(childNode);
SortNodes(node.ChildNodes);
}
}
}


private void SortNodes(TreeNodeCollection nodes)
{
if (nodes == null || nodes.Count &lt; 2)
{
return;
}
for (int i = 0; i &lt; nodes.Count - 1; i++)
{
for (int j = 0; j &lt; nodes.Count - 1; j++)
{
TreeNode node = nodes[j];
TreeNode nextNode = nodes[j + 1];
if (node.Text.CompareTo(nextNode.Text) &gt; 0)
{
TreeNode tempNode = new TreeNode();
tempNode = nextNode;
nodes.Remove(nextNode);
nodes.AddAt(i, tempNode);
}
}
}
}


when i don't sort, everything is faster, but now with this sorting its very slow and end up timing out.

Thanks

Thank you for posting at Dotnetfunda
[Administrator]



Responses

Posted by: Abhi2434 on: 2/8/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down

Resolved
Posted the same question here too :
http://www.codeproject.com/Messages/3359737/Treeview-Sorting-Perfomance.aspx

Well, I guess, it is better to sort these when Treeviewnode is expanded. Do not load the whole tree at a time, rather load on demand.

This will help you a lot.
:)

www.abhisheksur.com

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/8/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day Abhi2434

Thanks for your Reply. i posted on both to hear different opinions. But i think i resolved the Problem by sorting the nodes from the Database and it is fast as lightning again.

Thank you

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response