SortableBindingList的字母数字排序

时间:2016-05-31 15:07:43

标签: c# .net winforms sorting datagridview

我为DataGridView实现了一个SortableBindingList。在一个String的特定列上,我想扩展排序功能,使其包含字母数字排序。例如,项目1a,10a,9a将按如下方式分类:1a 9a 10a。

我的SortableBindingList在下面实现。我会在哪里自定义这个以实现一个特定列的字母数字排序?

//implement sorting for BindingList so columns can be sorted in datagridview.
    public class SortableBindingList<T>: BindingList<T>
    {
        protected override bool SupportsSortingCore
        {
            get
            {
                return true;
            }
        }

        protected override bool IsSortedCore
        {
            get
            {
                for (int i = 0; i < Items.Count - 1; ++i)
                {
                    T lhs = Items[i];
                    T rhs = Items[i + 1];
                    PropertyDescriptor property = SortPropertyCore;
                    if (property != null)
                    {
                        object lhsValue = lhs == null ? null :
property.GetValue(lhs);
                        object rhsValue = rhs == null ? null :
property.GetValue(rhs);
                        int result;
                        if (lhsValue == null)
                        {
                            result = -1;
                        }
                        else if (rhsValue == null)
                        {
                            result = 1;
                        }
                        else
                        {
                            result =
Comparer.Default.Compare(lhsValue, rhsValue);
                        }
                        if (SortDirectionCore ==
ListSortDirection.Descending)
                        {
                            result = -result;
                        }
                        if (result >= 0)
                        {
                            return false;
                        }
                    }
                }
                return true;
            }
        }

        private ListSortDirection sortDirection;
        protected override ListSortDirection SortDirectionCore
        {
            get
            {
                return sortDirection;
            }
        }


        private PropertyDescriptor sortProperty;
        protected override PropertyDescriptor SortPropertyCore
        {
            get
            {
                return sortProperty;
            }
        }


        protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
        {
            sortProperty = prop;
            sortDirection = direction;

            List<T> list = (List<T>)Items;
            list.Sort(delegate(T lhs, T rhs)
            {
                if (sortProperty != null)
                {
                    object lhsValue = lhs == null ? null :
sortProperty.GetValue(lhs);
                    object rhsValue = rhs == null ? null :
sortProperty.GetValue(rhs);
                    int result;
                    if (lhsValue == null)
                    {
                        result = -1;
                    }
                    else if (rhsValue == null)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = Comparer.Default.Compare(lhsValue,
rhsValue);
                    }
                    if (sortDirection == ListSortDirection.Descending)
                    {
                        result = -result;
                    }
                    return result;
                }
                else
                {
                    return 0;
                }
            });
        }


        protected override void RemoveSortCore()
        {
            sortDirection = ListSortDirection.Ascending;
            sortProperty = null;
        } 


    }

0 个答案:

没有答案