Spread Windows Forms 12.0 Product Documentation
Sorting Rows, Columns, or Ranges
Spread Windows Forms 12.0 Product Documentation > Developer's Guide > Customizing Row or Column Interaction > Managing Sorting of Rows of User Data > Sorting Rows, Columns, or Ranges

You can sort entire rows or columns in a sheet using code or the Spread Designer. To sort all the rows of an entire sheet based on the values of a given column is the most common case, but Spread allows you to sort either rows or columns and to specify which column or row to use as a key for sorting. This sort applies to the entire sheet.

Use the SortColumns (or SortRows) method to sort the arrangement of columns (or rows) in a sheet using one or more rows (or columns) as the key. This does not affect the data model, only how the data is displayed. Several overloads provide different ways to sort the columns (or rows). To further customize the way sorting is performed, use the SortInfo object in conjunction with these methods.

You can sort data in a range of cells without re-arranging the entire row or column in a sheet. This may be useful when, for example, you wish to arrange many rows in order of quantity but not include in the sort the final row that contains the totals of those quantities. In this case, you would sort the data in a range of cells but leave the final row, the bottom line, unsorted.

There are two ways to sort data in a range. For bound data, use the SortRows and SortColumns methods using the specified parameters in the overloads to specify which range of rows or columns to sort. For unbound data, use the SortRange method. For more information on sorting using the Spread Designer at design time, refer to the Using the Spread Designer procedure below.

The SortRange method is for unbound data only. This method sorts the data in a range of cells by moving the data around in the data model and moving the cell-level styles along with it. This method is not intended for bound data, as it moves data (not necessarily by entire row or column) and has the effect of moving the data around in the data source.

With the sortInfo array of the SortRange method, you can specify multiple criteria for sorting the data. This method gives you the ability to sort (or arrange) data in a smaller subset than entire rows or columns in a sheet. For more information, refer to the SortInfo object.

Using Code

To sort rows, use the SortRows method; to sort columns, use the SortColumns method.

Example

This example sorts all the rows in the sheet according to the values in the second column. Since column index is zero-based, the second column is 1. The sort indicator is turned on.

C#
Copy Code
fpspread1.ActiveSheet.SortRows(1,true,true);
VB
Copy Code
fpSpread1.ActiveSheet.SortRows(1,True,True)

Example

This example sorts rows 12 to 230 using a predefined array of sort information.

C#
Copy Code
FarPoint.Win.Spread.SortInfo[] sorter = new FarPoint.Win.Spread.SortInfo[1];
sorter[0] = new FarPoint.Win.Spread.SortInfo(0, false, System.Collections.Comparer.Default);
fpSpread1.ActiveSheet.SortColumns(12,230,sorter);
VB
Copy Code
Dim sorter(1) As FarPoint.Win.Spread.SortInfo
sorter(0) = New FarPoint.Win.Spread.SortInfo(0, False, System.Collections.Comparer.Default)
fpSpread1.ActiveSheet.SortColumns(12,230,sorter)

Using the Spread Designer

  1. At design time you can sort data using the Spread Designer in a very flexible way. Select the cells you want to sort, either by dragging over the cells or selecting the row or column headers.
  2. From the Data menu, select Sort. The Sort dialog is displayed.
  3. In the Sort dialog, select the options you would like and click OK.
  4. From the File menu choose Apply and Exit to apply your changes to the component and exit Spread Designer.