在IBindingListView中实现Sort方法 [英] Implementing Sort method in IBindingListView

查看:108
本文介绍了在IBindingListView中实现Sort方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BindingList< t>实现了排序方法.和IBindingListView

I implemented the sort method using BindingList<t> and IBindingListView

	protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
	{
            //Input validation
            if (property == null)
                return;

            List<t> list = (List<t>)Items;
            _comparers = new List<propertycomparer><t>>();

            _comparers.Add(new PropertyComparer<t>(property, direction));            

            list.Sort(CompareValuesByProperties);

            //Save the sorting properties
            _isSorted = true;
            _sortProperty = property;
            _sortDirection = direction;
        }

	private int CompareValuesByProperties(T object1, T object2)
        {
            if (object1 == null)
                return (object2 == null) ? 0 : -1;
            else
            {
                if (object2 == null)
                    return 1;
                else
                {
                    foreach (PropertyComparer<t> comparer in _comparers)
                    {
                        int result = comparer.Compare(object1, object2);
                        if (result != 0)
                            return result;
                    }
                    return 0;
                }
            }
        }


	public int Compare(T object1, T object2)
        {

            // Get property values
            object object1Value = GetPropertyValue(object1, _property.Name);
            object object2Value = GetPropertyValue(object2, _property.Name);

            if (object1Value == object2Value)
                return 5;

            // Calling the sort with compare according to the sort direction.
            if (_direction == ListSortDirection.Ascending)
            {
                return CompareAscending(object1Value, object2Value);
            }
            else
            {
                return CompareDescending(object1Value, object2Value);
            }
        }

	private int CompareAscending(object value1, object value2)
        {
            //NOTE:
            //value1 = null and value2 = null returns 0
            //value1 = null and value2 = not null returns -1
            //value1 = not null and value2 = null returns 1
            //value1 = not null and value2 = not null continue the comparision
            if (value1 == null)
                return (value2 == null ? 0 : -1);
            else
                if (value2 == null)
                    return 1;

            int result;

            // If values implement IComparer
            if (value1 is IComparable)
            {
                result = ((IComparable)value1).CompareTo(value2);
            }
            // If values are equivalent
            else if (value1.Equals(value2))
            {
                result = 0;
            }
            else result = ((IComparable)value1).CompareTo(value2);

            return result;
        }
</t></t></t></propertycomparer></t></t>




现在,我使用员工类创建员工对象.它具有empId,Name,Section作为属性.




Now I create employee objects by using employee class. It has empId, Name,Section as properties.

Employee e1=new Employee("1","A1","IT");
Employee e1=new Employee("2","A2",IT");
Employee e1=new Employee("3","A2","IT");



现在,我将那些对象绑定到gridview.现在,如果我单击网格上的empId标头,它将根据empId进行排序.但是我的问题是,如果我单击Section标头,它将根据名称在"IT"部分中进行排序.之后,当我单击该行时,它也会对网格进行排序.



Now I bind those object to a gridview. Now If I click on empId header on the grid it will sort according to the empId. But my problem is if I click on Section header it will sort according to the name as all in the "IT" section. And after that when I''m clicking on the row also it sorts the grid.

推荐答案

对您可能有用的链接对-
如何在自定义数据绑定中允许按多列排序 [ http://blw.sourceforge.net/ [ ^ ]
Couple of links that might be useful to you -
How To Allow To Sort By Multiple Columns in Custom Data Binding[^]
http://blw.sourceforge.net/[^]


这篇关于在IBindingListView中实现Sort方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆