$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
WinForms TreeView Control
Build complex navigation systems and display complex hierarchical structures
Home > WinForms UI Controls > User Manual > Create Custom Sorting for Nodes in C#

C# Customize Sorting Nodes of KetticTreeView

KetticTreeView WinForms control offers the support of sorting nodes and the rich C# API to manipulate the data. It is easy to perform the KetticTreeView node sorting operations on the fly when we exposed the filter event in C# Windows project. The rich C# API and complete set of filter events included in the KetticTreeView component help the users to fully control the filtering functionalities for nodes. The developers can easily add, delete, update items at runtime as well as create hierarchical navigation for nodes of KetticTreeView control.

Create Custom Sorting Nodes in KetticTreeView in C#.NET

KetticTreeView control for Windows Forms provides large flexibility for the users to create custom sorting logic to apply to the Nodes of KetticTreeView. And the KetticTreeView control provides higher priority than the sorting by default. The custom sorting logic can be applied to nodes through creating a C# class to inherit the TreeNodeComparer and override the Compare method.
In the C# code sample below, we will create a custom comparer and reverse the sorting order of KetticTreeView. It will sort in ascending order as we sort the Nodes of KetticTreeView.

class MyComparer : TreeNodeComparer
{
public MyComparer(KetticTreeViewElement treeView)
: base(treeView)
{

}

public override int Compare(KetticTreeNode x, KetticTreeNode y)
{
if (this.TreeViewElement.SortOrder == SortOrder.Descending)
{
return x.Text.CompareTo(y.Text);
}

return x.Text.CompareTo(y.Text) * -1;
}
}
After we create the custom comparer, we can add it to the KetticTreeView in C#.NET project by using the C# code below

ketticTreeView1.TreeViewElement.Comparer = new MyComparer(this.ketticTreeView1.TreeViewElement);
And then we can run the application to check if we can reverse the sorting order. This is achieved by adding a button and a label to the Windows Forms to customize and print the node sorting order. The following C# code snippet demonstrates the customization of testing sorting nodes of KetticTreeView.

private void ketticButton1_Click(object sender, EventArgs e)
{
if (ketticTreeView1.SortOrder == SortOrder.None)
{
ketticTreeView1.SortOrder = SortOrder.Ascending;
ketticLabel1.Text = "Sorting: Ascending";
}
else if (ketticTreeView1.SortOrder == SortOrder.Ascending)
{
ketticTreeView1.SortOrder = SortOrder.Descending;
ketticLabel1.Text = "Sorting: Descending";
}
else
{
ketticTreeView1.SortOrder = SortOrder.None;
ketticLabel1.Text = "Sorting: None";
}
}
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls