Sorting Data

C1FlexGrid supports sorting data directly using code. The following example code was taken from the sorting & filtering sample in the ControlExplorer installed with this product.

Note: The ControlExplorer sample is located in the C:\Documents and Settings\<username>\My Documents\ComponentOne Samples\Studio for Windows Phone or C:\Users\<username>\Documents\ComponentOne Samples\Studio for Windows Phone folder.

In this example, CollectionViewICollectionView.SortDescriptions are used to change the sort direction when the column header is tapped.

void flexgrid_Tap(object sender, C1GestureEventArgs e)

        {

            // detect tap and sort only if a column header was tapped

            var testInfo = flexgrid.HitTest(e);

            if (testInfo.CellType == CellType.ColumnHeader)

            {

                // get current column header property name

                SortGrid(flexgrid.Columns[testInfo.Column].BoundPropertyName);

            }

        }

 

   void SortGrid(string propertyName)

        {

            // If we have a sort description with the current property path, then we remove it from the collection and

            // add a new one with the opposite sort direction (if we tap a column header twice, the sort direction changes).

            // If we don´t have a sort description with the current property path, then we add it with ascending sort direction.

            var sortDescriptions = flexgrid.CollectionView.SortDescriptions;

 

            using (flexgrid.CollectionView.DeferRefresh())

            {

                var sortDescription = sortDescriptions.FirstOrDefault(sd => sd.PropertyName.Equals(propertyName));

 

                // if multiple selection is disabled, we always clear the collection

                if (!_allowMultipleSort)

                {

                    sortDescriptions.Clear();

                }

 

                if (string.IsNullOrEmpty(sortDescription.PropertyName))

                {

                    sortDescription.PropertyName = propertyName;

                    sortDescription.Direction = ListSortDirection.Ascending;

                    sortDescriptions.Add(sortDescription);

                }

                else

                {

                    sortDescriptions.Remove(sortDescription);

                    sortDescriptions.Add(newSortDescription(propertyName, sortDescription.Direction == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending));

                }

            }

        }

You can disable grid sorting by setting the AllowSorting property to false, or disable it for specific columns by setting the Column.AllowSorting property to false.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.