繁体   English   中英

使用SortableBindingList和DataGridView [C#]排序

[英]Sorting with SortableBindingList and DataGridView [C#]

我正在尝试解决使用绑定列表对datagridview排序的问题。

我已经实现了SortableBindingList,当用户单击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());
        }
    }
}

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

任何帮助,将不胜感激。

您必须在您的公共方法中获取PropertyDescriptor

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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