使用SortableBindingList和DataGridView [C#]排序



我正在尝试使用绑定列表来解决数据杂志的问题。

我已经实现了一个可排序的列表,当用户单击datagridview列标头进行排序时,这很好。

但是,我想在加载表单/网格时添加"默认值"排序,但我似乎无法解决此问题。我看过这个示例,但似乎不起作用。

我想实现的目标:

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

我知道我必须在sortable bindinglist类中创建一个公共方法来传递列并分类方向。

任何帮助将不胜感激。

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

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