使用SortableBindingList和DataGridView[C#]进行排序

使用SortableBindingList和DataGridView[C#]进行排序,c#,sorting,datagridview,C#,Sorting,Datagridview,我试图解决使用绑定列表对datagridview进行排序的问题 我已经实现了一个SortableBindingList,当用户单击datagridview列标题进行排序时,它非常有效 然而,我想在加载表单/网格时添加一个“默认”排序,我似乎无法找到解决这个问题的方法。我见过,但似乎不起作用 我想要实现的伪代码: SortableBindingList<T> TestList = new SortableBindingList<T>(); //Load data to t

我试图解决使用绑定列表对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
    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 TestList=新建SortableBindingList();
//在这里将数据加载到TestList
TestList.Sort(“Column1”,ListSortDirection.升序);//按第1列排序
gridTest.DataSource=TestList;//将测试列表分配给网格数据源
可排序绑定列表:

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
    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());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
名称空间列表
{
公共类SortableBindingList:BindingList,其中T:class
{
私人住宅被分类;
私有ListSortDirection\u sortDirection=ListSortDirection.Ascending;
私有财产描述人财产;
公共SortableBindingList()
{
}
公共SortableBindingList(IList列表)
:基本(列表)
{
}
受保护的覆盖布尔支持SSortingCore
{
获取{return true;}
}
受保护的覆盖布尔IsSortedCore
{
获取{return\u isSorted;}
}
受保护的覆盖列表SORTDirection SortDirectionCore
{
获取{return\u sortDirection;}
}
受保护的重写属性描述符SortPropertyCore
{
获取{return\u sortProperty;}
}
受保护的覆盖void RemoveSortCore()
{
_sortDirection=列表sortDirection.升序;
_sortProperty=null;
_isSorted=false;//谢谢Luca
}
受保护的覆盖无效ApplySortCore(PropertyDescriptor属性、ListSortDirection方向)
{
_sortProperty=prop;
_排序方向=方向;
列表=作为列表的项目;
if(list==null)返回;
列表。排序(比较);
_isSorted=true;
//触发列表已更改的事件。
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
专用整数比较(T左、T右)
{
var结果=对照(lhs、rhs);
//下降时反转
if(_sortDirection==ListSortDirection.Descending)
结果=-结果;
返回结果;
}
私人国际比较(左、右)
{
对象lhsValue=lhs==null?null:\u sortProperty.GetValue(lhs);
对象rhsValue=rhs==null?null:\u sortProperty.GetValue(rhs);
if(lhsValue==null)
{
返回值(rhsValue==null)?0:-1;//null相等
}
if(rhsValue==null)
{
返回1;//第一个有值,第二个没有值
}
if(LHS值是可比较的)
{
返回((IComparable)LHS值)。比较(rhsValue);
}
if(lhsValue.Equals(rhsValue))
{
返回0;//两者相同
}
//不可比较,与字符串比较
返回lhsValue.ToString().CompareTo(rhsvvalue.ToString());
}
}
}
我知道我必须在SortableBindingList类中创建一个公共方法来传递列和排序方向


任何帮助都将不胜感激。

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

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

这可能不应该被标记为vb…@Danielshillock更新。所以我必须把它作为推荐标签添加进去。这正是我需要的。非常感谢。GetProperties()是我余生都会记住的东西。