Grid > Data Processing > Sorting |
Sorting is the process of sorting the rows that reside on the same hierarchical level. The grid support multiple sorting rules that define a certain sorting order of the rows that reside at the same level in the grid grouping or self-referencing level.
Sorting rules are represented by instances of the NSortingRule class that reside in a NSortingRuleCollection accessible from the NGrid.SortingRules property. In most cases you will create filter rules that are associated with grid columns - the following code example first sorts by the Name and then by the Sales column:
Multiple Sorting Rules |
Copy Code
|
---|---|
NSortingRule sortByName = new NSortingRule(grid.Columns.GetColumnByFieldName("Name")); grid.SortingRules.Add(sortByName); NSortingRule sortBySales = new NSortingRule(grid.Columns.GetColumnByFieldName("Sales")); grid.SortingRules.Add(sortBySales); |
Sorting can be visually performed by the user in several ways:
Each sorting rule has an assigned Row Value that associates each row with a specific value, by which the rule sorts the rows. The Row Value is specified by the RowValue property. Additionally each sorting rule has a sorting direction which is specified by the rule Sorting Direction property. The following code example creates a sorting rule and modifies its row value and sorting rule, so that the rule sorts the rows by the Name field in Descending order:
Sorting Rule Row Value and Sorting Direction |
Copy Code
|
---|---|
NSortingRule sortByName = new NSortingRule(); sortByName.RowValue = new NFieldRowValue("Name"); sortByName.SortingDirection = ENSortingDirection.Ascending; grid.SortingRules.Add(sortByName); |
Each sorting rule can be associated with a grid column. The associated column is specified by the NSortingRule.Column property. There are several things, which happen when a sorting rule is associated with a column:
Sort By Column |
Copy Code
|
---|---|
// create a sorting rule that sorts by the Total column. // Note that when the sorting rule is associated with a column, by default it sorts by it // you can still however set the RowValue to alter the default column sorting, yet keep the Column association. NSortingRule sortByTotal = new NSortingRule(); sortByTotal.Column = totalColumn; grid.SortingRules.Add(sortByTotal); |
Column Header Sorting Button | Column Context Menu |
---|---|
![]() |
![]() |
When the user clicks on the sorting button of a column header, the grid asks the column to create a sorting rule for that column. By default the column creates a sorting rule that sorts by the column value (i.e. a sorting rule that is simply associated with the column). You may however want to create a custom sorting rule when the user clicks on the column header sorting button. This is achieved by handling the NColumn.CreateSortingRuleDelegate delegate, like shown in the following example:
Custom sorting rules created by columns |
Copy Code
|
---|---|
column.CreateSortingRuleDelegate = delegate(NColumn theColumn) { // create a custom sorting rule NSortingRule sortingRule = new NSortingRule(); NCustomRowValue<double> customRowValue = new NCustomRowValue<double>(); customRowValue.GetRowValueDelegate = delegate(NCustomRowValueGetRowValueArgs<double> args) { NDataSource dataSource = args.DataSource; return (double)((double)dataSource[args.Row, "Price"] * (int)dataSource[args.Row, "Quantity"]); }; sortingRule.RowValue = customRowValue; return sortingRule; }; |