Click here to Skip to main content
14,878,669 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I implemented the sort method using BindingList<t> and IBindingListView

C#
	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>



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

C#
Employee e1=new Employee("1","A1","IT");
Employee e1=new Employee("2","A2",IT");
Employee e1=new Employee("3","A2","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.
Posted

1 solution

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900