简体   繁体   中英

Sorting with SortableBindingList and DataGridView [C#]

I'm trying to tackle the issue of sorting a datagridview using a binding list.

I've implemented a SortableBindingList and this works great when a user clicks the datagridview column header to sort.

However I would like to add a 'default' sort on loading of the form/grid and I can't seem to figure a way around this. I've seen this example , but it doesn't seem to work.

Pseudocode of what I would like to achieve:

SortableBindingList<T> TestList = new SortableBindingList<T>();

//Load data to the TestList here

TestList.Sort("Column1", ListSortDirection.Ascending); // Sort by Column 1

gridTest.DataSource = TestList ; // Assign TestList to grid datasource

SortableBindingList:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    namespace Lists
    {

    public class SortableBindingList<T> : BindingList<T> where T : class
    {
        private bool _isSorted;
        private ListSortDirection _sortDirection = ListSortDirection.Ascending;
        private PropertyDescriptor _sortProperty;


        public SortableBindingList()
        {
        }

        public SortableBindingList(IList<T> list)
            : base(list)
        {
        }

        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        protected override bool IsSortedCore
        {
            get { return _isSorted; }
        }

        protected override ListSortDirection SortDirectionCore
        {
            get { return _sortDirection; }
        }

        protected override PropertyDescriptor SortPropertyCore
        {
            get { return _sortProperty; }
        }

        protected override void RemoveSortCore()
        {
            _sortDirection = ListSortDirection.Ascending;
            _sortProperty = null;
            _isSorted = false; //thanks Luca
        }

        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            _sortProperty = prop;
            _sortDirection = direction;

            List<T> list = Items as List<T>;
            if (list == null) return;

            list.Sort(Compare);

            _isSorted = true;
            //fire an event that the list has been changed.
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }


        private int Compare(T lhs, T rhs)
        {
            var result = OnComparison(lhs, rhs);
            //invert if descending
            if (_sortDirection == ListSortDirection.Descending)
                result = -result;
            return result;
        }

        private int OnComparison(T lhs, T rhs)
        {
            object lhsValue = lhs == null ? null : _sortProperty.GetValue(lhs);
            object rhsValue = rhs == null ? null : _sortProperty.GetValue(rhs);
            if (lhsValue == null)
            {
                return (rhsValue == null) ? 0 : -1; //nulls are equal
            }
            if (rhsValue == null)
            {
                return 1; //first has value, second doesn't
            }
            if (lhsValue is IComparable)
            {
                return ((IComparable)lhsValue).CompareTo(rhsValue);
            }
            if (lhsValue.Equals(rhsValue))
            {
                return 0; //both are the same
            }
            //not comparable, compare ToString
            return lhsValue.ToString().CompareTo(rhsValue.ToString());
        }
    }
}

I understand I would have to create a public method in the SortableBindingList class to pass the column and sort direction.

Any help would be appreciated.

You have to get a PropertyDescriptor in your public method:

public void Sort(string propertyName, ListSortDirection direction)
{
    this.ApplySortCore(TypeDescriptor.GetProperties(typeof(T))[propertyName], direction);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM