| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4015 人关注过本帖
标题:winForm中DataGridView控件自定义排序的问题
只看楼主 加入收藏
snowyezi
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-9-9
收藏
得分:0 
IComparable这个接口我也试过了,同样不行哟
2010-09-10 15:49
saitor
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:5
帖 子:373
专家分:1520
注 册:2009-5-18
收藏
得分:0 
你LIST里放的是什么
2010-09-10 15:54
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:4 
如采用集合绑定,则排序失效.需自己去实现

C#超级群 74862681,欢迎大家的到来!
2010-09-10 22:26
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
程序代码:
 this.dgvRoom.DataSource = new SortList<RoomType>(roomTypeManager.GetAllRoomType());

        private class SortList<T> : BindingList<T>
        {
            private bool isSortedCore = true;
            private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
            private PropertyDescriptor sortPropertyCore = null;
            private string defaultSortItem;

            public SortList() : base() { }
            public SortList(IList<T> list) : base(list) { }

            //重写父类方法:是否支持排序
            protected override bool SupportsSortingCore
            {
                get
                {
                    return true;
                }
            }
            //是否支持搜索
            protected override bool SupportsSearchingCore
            {
                get
                {
                    return true;
                }
            }
            //是否已经排序
            protected override bool IsSortedCore
            {
                get
                {
                    return this.isSortedCore;
                }
            }
            //排序的方向
            protected override ListSortDirection SortDirectionCore
            {
                get
                {
                    return this.sortDirectionCore;
                }
            }
            //获取用于对列表排序的属性说明符
            protected override PropertyDescriptor SortPropertyCore
            {
                get
                {
                    return this.sortPropertyCore;
                }
            }
            protected override int FindCore(PropertyDescriptor prop, object key)
            {
                for (int i = 0; i < this.Count; i++)
                {
                    if (Equals(prop.GetValue(this[i]),key)) return i;
                }
                return 1;
            }
            //对项进行排序
            protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
            {
                this.isSortedCore = true;
                this.sortDirectionCore = direction;
                this.sortPropertyCore = prop;
                Sort();
            }
            //移除
            protected override void RemoveSortCore()
            {
                if (this.isSortedCore)
                {
                    this.isSortedCore = false;
                    this.sortPropertyCore = null;
                    this.sortDirectionCore = ListSortDirection.Descending;
                    Sort();
                }
            }
            //默认排序属性
            private string DefaultSortItem
            {
                get { return this.defaultSortItem; }
                set { if (defaultSortItem != value) defaultSortItem = value; Sort(); }
            }
            private void Sort()
            {
                List<T> list = (this.Items as List<T>);
                list.Sort(CompareCore);
                ResetBindings();
            }
            //比较
            private int CompareCore(T o1, T o2)
            {
                int ret = 0;
                if (SortPropertyCore != null)
                {
                    ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
                }
                if (ret == 0 && DefaultSortItem != null)
                {
                    PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
                    if (property != null)
                    {
                        ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
                    }
                }
                if(SortDirectionCore==ListSortDirection.Descending) ret=-ret;
                return ret;
            }
            private static int CompareValue(object o1,object o2,Type type)
            {
                if (o1 == null) return o2 == null ? 0 : -1;
                else if (o2 == null) return 1;
                else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
                else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(Convert.ToDateTime(o2));
                else if (type == typeof(Int32)) return Convert.ToInt32(o1).CompareTo(Convert.ToInt32(o2));
                else return (o1.ToString().Trim(), o2.ToString().Trim());
            }
        }

C#超级群 74862681,欢迎大家的到来!
2010-09-10 22:27
快速回复:winForm中DataGridView控件自定义排序的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022469 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved