FlexGrid, by default, allows end-users to apply sorting on a single column by clicking the column header in ascending or descending order. However, the grid also provides you flexibility, so that you can sort your data according to your requirement. Below sections take you through the ways to perform various operations related to sorting
You can apply sorting through code by calling Sort method of the C1FlexGrid class. The method takes SortFlags enumeration as its parameter which lets you provide the various sort options such as setting the sort order and ignore casing. The method has various overloads which gives you flexibility to apply sorting either on a column, or a range of cells, rows or columns.
Use the code below to sort columns and apply sorting options through code in the WinForms FlexGrid.
To apply sorting on multiple columns through code, you can use the Sort property of Column class and then call the Sort() method with SortFlags set to UseColSort. The Sort property accepts values from SortFlags enumeration.
Following code shows how to sort multiple columns of the WinForms FlexGrid through code.
To allow user to sort multiple columns at runtime, set AllowSorting property of the C1FlexGrid class to MultiColumn. The property accepts values from the AllowSortingEnum enumeration.
Following code shows how to let user sort multiple columns of the WinForms FlexGrid at run-time.
To remove sorting from the grid, you can set SortDefinition property of the C1FlexGrid class to an empty string.
Below code how to remove sorting from the WinForms FlexGrid.
To disable sorting on a particular column, you need to set the AllowSorting property of that Column object to false.
Use the code below to disable sorting on a particular column of the WinForms FlexGrid.
Order of sorting usually varies in case of bound and unbound mode. When a column header is clicked in case of bound mode, sorting is done same as DefaultView.Sort property of the data table. However, in case of unbound mode, column is sorted either according to String.Compare method or by differentiating the lower and upper case according to the culture.
Refer to the code below to specify sorting order of a WinForms FlexGrid column.
FlexGrid provides several sorting options which are required for most commonly used scenarios such as ignore case, use the display value etc. However, if you need to have more flexibility and control over sort operation, you can even write your own custom logic using the IComparer class. For instance, the example below sorts the Name column by file extensions. In the sample code, custom logic to sort by file extension is implemented in the FileNameComparer class which is then passed as parameter to Sort() method of the C1FlexGrid class.
The code below demonstrates how to apply custom sorting on the WinForms FlexGrid columns.
For detailed implementation of custom sorting, see the product sample named Custom Sort.