使用SortableBindingList和DataGridView排序[C#]

时间:2017-04-25 14:20:22

标签: c# sorting datagridview

我试图解决使用绑定列表对datagridview进行排序的问题。

我已经实现了SortableBindingList,当用户单击datagridview列标题进行排序时,这非常有用。

但是我想添加一个'默认'在加载表单/网格时排序,我似乎无法解决这个问题。我看过this example,但它似乎无法发挥作用。

我希望实现的伪代码:

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());
        }
    }
}

我知道我必须在SortableBindingList类中创建一个公共方法来传递列和排序方向。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您必须在公共方法中获得PropertyDescriptor

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