Chinaunix首页 | 论坛 | 博客
  • 博客访问: 683579
  • 博文数量: 103
  • 博客积分: 2532
  • 博客等级: 大尉
  • 技术积分: 2039
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-26 16:07
文章分类

全部博文(103)

文章存档

2012年(38)

2011年(28)

2010年(16)

2009年(16)

2008年(5)

分类: C/C++

2010-12-01 09:55:07

首先,实现ICompare接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Models
{
    public class ObjectPropertyCompare : IComparer
    {
        private PropertyDescriptor property;
        private ListSortDirection direction;

        // 构造函数
        public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
        {
            this.property = property;
            this.direction = direction;
        }

        // 实现IComparer中方法
        public int Compare(T x, T y)
        {
            object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
            object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);

            int returnValue;

            if (xValue is IComparable)
            {
                returnValue = ((IComparable)xValue).CompareTo(yValue);
            }
            else if (xValue.Equals(yValue))
            {
                returnValue = 0;
            }
            else
            {
                returnValue = xValue.ToString().CompareTo(yValue.ToString());
            }

            if (direction == ListSortDirection.Ascending)
            {
                return returnValue;
            }
            else
            {
                return returnValue * -1;
            }
        }
    }
}

 

然后创建自定义的类,实现IBindingList接口,为方便起见,这里直接继承BindingList类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Models
{
    ///


    /// 自定义绑定列表类
    ///

    /// 列表对象类型
    public class BindingCollection : BindingList
    {
        private bool isSorted;
        private PropertyDescriptor sortProperty;
        private ListSortDirection sortDirection;

        ///


        /// 构造函数
        ///

        public BindingCollection()
            : base()
        {

        }

        ///


        /// 构造函数
        ///

        /// IList类型的列表对象
        public BindingCollection(IList list)
            : base(list)
        {
        }

        ///


        /// 自定义排序操作
        ///

        ///
        ///
        protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            List items = this.Items as List;

            if (items != null)
            {
                ObjectPropertyCompare pc = new ObjectPropertyCompare(property, direction);
                items.Sort(pc);
                isSorted = true;
            }
            else
            {
                isSorted = false;
            }

            sortProperty = property;
            sortDirection = direction;

            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        ///


        /// 获取一个值,指示列表是否已排序
        ///

        protected override bool IsSortedCore
        {
            get
            {
                return isSorted;
            }
        }

        ///


        /// 获取一个值,指示列表是否支持排序
        ///

        protected override bool SupportsSortingCore
        {
            get
            {
                return true;
            }
        }

        ///


        /// 获取一个只,指定类别排序方向
        ///

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

        ///


        /// 获取排序属性说明符
        ///

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

        ///


        /// 移除默认实现的排序
        ///

        protected override void RemoveSortCore()
        {
            isSorted = false;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

    }
}

创建BindingCollection后即可直接应用:

原来的方式是:

IList list = new List();

...

dataGridView.DataSource = list;

现在只需更改最后一句为:

dataGridView.DataSource = new BindingCollection(list);

即可

阅读(6167) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~