FlexGrid allows you to sort grid's data using the following methods:
The image below shows how the FlexGrid appears after sorting is applied to the grid by tapping header of the column, Country.
C# |
Copy Code
|
---|---|
grid.AllowSorting = true;
|
XAML |
Copy Code
|
---|---|
<c1:FlexGrid AllowSorting="True">
|
The image below shows how the FlexGrid appears after sorting is applied to the column Name.
The following code example demonstrates how to sort a FlexGrid control in C# and XAML. This example uses the data source, Customer.cs created in the Quick start section.
using Xamarin.Forms;
using C1.CollectionView;
using C1.Xamarin.Forms.Grid;
C# |
Copy Code
|
---|---|
public static FlexGrid GetGrid() { var dataCollection = Customer.GetCustomerList(10); C1CollectionView<Customer> cv = new C1CollectionView<Customer>(dataCollection); var sort = cv.SortDescriptions.FirstOrDefault(sd => sd.SortPath == "Name"); var direction = sort != null ? sort.Direction : SortDirection.Descending; cv.SortAsync(x => x.Name, direction == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); FlexGrid _grid = new FlexGrid(); _grid.ItemsSource = cv; _grid.VerticalOptions = LayoutOptions.FillAndExpand; return _grid; cv.SortChanged += cv_SortChanged; } static void cv_SortChanged(object sender, EventArgs e) { throw new NotImplementedException(); } |
By default, sorting is applied on the bound field. However, you can sort a column by a different field. All you need to do is set the SortMemberPath property to the column by which you want the grid to be sorted. For example, the following column is bound to "FullName" but sorts by "LastName".
C# |
Copy Code
|
---|---|
column.Binding = “FullName”; column.SortMemberPath = “LastName”; |