Remove sorting of a flexgrid

Posted by: m-merciersynelia-fr on 27 September 2017, 9:07 pm EST

    • Post Options:
    • Link

    Posted 27 September 2017, 9:07 pm EST

    Hello,

    I have a flexgrid, wich has the AllowSorting property true. Every column of the flexgrid also have the AllowSorting property true.

    When i click on a column header, the column is sorted. OK, it’s normal.

    In my flexgrid, the user is allowed to update values, so my problem is that when a column is sorted, and a value is updated, the flexgrid sorts itself instantanately.

    I would like to be able to remove sorting informations of the flexgrid. I tried to use _flex.AllowSorting = false in the keydown event of the flexgrid, but it doesn’t work. As soon as i leave the cell, the flexgrid sorts itself again.

    Do you have an idea to solve my problem ?

  • Posted 27 September 2017, 9:07 pm EST

    Hi,

    In bound mode sorting is done automatically after list changes. This is why when any update is performed sorting is done automatically. This is a .NET behavior. In order for us to have a behavior where sorting is done only on request and that too in bound mode. We would have to create a custom DataSource. Thus we have following requirements from our DataSource:

    1. List should be sortable for every items.
    2. List should not sort when list changes.

    Here is our custom DataSource:

    SortableBindingList

    [csharp]

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Linq;

    public class SortableBindingList : BindingList

    {

    private bool isSortedValue;

    ListSortDirection sortDirectionValue;

    PropertyDescriptor sortPropertyValue;

    public SortableBindingList()
    {
    }
    
    public SortableBindingList(IList<T> list)
    {
        foreach (object o in list)
        {
            this.Add((T)o);
        }
    }
    
    protected override void ApplySortCore(PropertyDescriptor prop,
        ListSortDirection direction)
    {
        Type interfaceType = prop.PropertyType.GetInterface("IComparable");
    
        if (interfaceType == null && prop.PropertyType.IsValueType)
        {
            Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
            if (underlyingType != null)
            {
                interfaceType = underlyingType.GetInterface("IComparable");
            }
        }
    
        if (interfaceType != null)
        {
            sortPropertyValue = prop;
            sortDirectionValue = direction;
            IEnumerable<T> query = base.Items;
            if (direction == ListSortDirection.Ascending)
            {
                query = query.OrderBy(i => prop.GetValue(i));
            }
            else
            {
                query = query.OrderByDescending(i => prop.GetValue(i));
            }
    
            int newIndex = 0;
            foreach (object item in query)
            {
                this.Items[newIndex] = (T)item;
                newIndex++;
            }
    
            isSortedValue = true;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
        else
        {
            throw new NotSupportedException("Cannot sort by " + prop.Name +
                ". This" + prop.PropertyType.ToString() +
                " does not implement IComparable");
        }
    }
    
    protected override PropertyDescriptor SortPropertyCore
    {
        get { return sortPropertyValue; }
    }
    
    protected override ListSortDirection SortDirectionCore
    {
        get { return sortDirectionValue; }
    }
    
    protected override bool SupportsSortingCore
    {
        get { return true; }
    }
    
    protected override bool IsSortedCore
    {
        get { return isSortedValue; }
    }
    
    protected override void OnListChanged(ListChangedEventArgs e)
    {
        base.OnListChanged(e);
        // Do not sort the list
    }
    

    }[/csharp]

    Please find the attached sample with complete implementation.

    ~nilay

    2016/11/RemoveSortingOfAFlexgrid.zip

  • Posted 30 January 2019, 6:03 am EST

    Hi,

    I have the same problem

    but I bound datatable to the flexGrid

    So I can’t use the SortableBindingList

    Do you have please another idea for me?

    Thanks,

    Yonit

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels