我实现了sort方法使用BindingList和IBindingListView
现在我使用Employee类创建Employee对象。它具有EMPID,作为属性名,第
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>
{C}
现在,我的对象绑定到GridView。现在如果我点击上对电网EMPID头,将排序根据的EMPID的。但我的问题是,如果我点击组头,将根据"IT"一节中的所有名称排序。后,当我点击该行也,各种电网。
| sanjeewabdissa