当大家自定义对象时,无论你返回Ilist 还是 自定义集合, 当绑定到datagridview 或者其他对象。 都无法实现标题排序。(即:click列头,自动排序), 我们需要怎么做才能排序呢, 需要继承什么接口呢。答案是继承 IBindingList 接口。
public class model
{
public int Id
{ get; set; }
public string Name
{ get; set; }
}
public class ModelCollection:IList,IBindingList, IEnumerator
{
private ArrayList fileLists = new ArrayList();
private ListSortDirection _direction;
private PropertyDescriptor _property;
public ModelCollection()
{
}
#region IList Members
public int Add(object value)
{
throw new NotImplementedException();
}
public int Add(int id, string name)
{
model m = new model();
m.Id = id;
m.Name = name;
return fileLists.Add(m);
}
public int Add(model value)
{
return fileLists.Add(value);
}
public void Clear()
{
fileLists.Clear();
}
public bool Contains(object value)
{
throw new NotImplementedException();
}
public int IndexOf(object value)
{
throw new NotImplementedException();
}
public void Insert(int index, object value)
{
throw new NotImplementedException();
}
public bool IsFixedSize
{
get { return IsFixedSize; }
}
public bool IsReadOnly
{
get { return fileLists.IsReadOnly; }
}
public void Remove(object value)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public object this[int index]
{
get
{
if ((index >= 0) && (index < fileLists.Count))
{
return fileLists[index];
}
else
{
throw new System.Exception("Index must be between 0 and " +
fileLists.Count.ToString() + ".");
}
}
set
{
fileLists[index] = value;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public int Count
{
get { return fileLists.Count; }
}
public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}
public object SyncRoot
{
get { throw new NotImplementedException(); }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new FileListCollectionEnumerator(this);
}
// Inner class implements IEnumerator interface:
private class FileListCollectionEnumerator : IEnumerator
{
private int position = -1;
private ModelCollection p;
public FileListCollectionEnumerator(ModelCollection p)
{
this.p = p;
}
// Declare the MoveNext method required by IEnumerator:
public bool MoveNext()
{
if (position < p.Count - 1)
{
position++;
return true;
}
else
{
return false;
}
}
// Declare the Reset method required by IEnumerator:
public void Reset()
{
position = -1;
}
// Declare the Current property required by IEnumerator:
public object Current
{
get
{
return p[position];
}
}
}
#endregion
#region IBindingList Members
public void AddIndex(PropertyDescriptor property)
{
throw new NotImplementedException();
}
public object AddNew()
{
model m = new model();
fileLists.Add(m);
return m;
}
public bool AllowEdit
{
get { return true; }
}
public bool AllowNew
{
get { return true; }
}
public bool AllowRemove
{
get { return true; }
}
public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
{
_property = property;
_direction = direction;
fileLists.Sort(new Comparer(property, direction));
}
public int Find(PropertyDescriptor property, object key)
{
throw new NotImplementedException();
}
public bool IsSorted
{
get { return _property != null; }
}
public event ListChangedEventHandler ListChanged;
public void RemoveIndex(PropertyDescriptor property)
{
throw new NotImplementedException();
}
public void RemoveSort()
{
_property = null;
}
public ListSortDirection SortDirection
{
get { return _direction; }
}
public PropertyDescriptor SortProperty
{
get { return _property;}
}
public bool SupportsChangeNotification
{
get { return true; }
}
public bool SupportsSearching
{
get { return true; }
}
public bool SupportsSorting
{
get { return true; }
}
#endregion
internal class Comparer : IComparer
{
private PropertyDescriptor _sortProperty;
private ListSortDirection _sortDescription;
public Comparer(PropertyDescriptor sortProperty, ListSortDirection sortDescription)
{
_sortProperty = sortProperty;
_sortDescription = sortDescription;
}
public int Compare(object x, object y)
{
Type propType = _sortProperty.PropertyType;
int sort = 0;
if (propType == typeof(string))
{
sort = string.Compare((string)_sortProperty.GetValue(x).ToString(),
(string)_sortProperty.GetValue(y).ToString());
}
else if (propType == typeof(DateTime))
{
sort = DateTime.Compare((DateTime)_sortProperty.GetValue(x),
(DateTime)_sortProperty.GetValue(y));
}
else if (propType.IsEnum)
{
sort = (int)_sortProperty.GetValue(x) - (int)_sortProperty.GetValue(y);
}
else if (propType == typeof(int))
{
sort = (int)_sortProperty.GetValue(x) - (int)_sortProperty.GetValue(y);
}
else if (propType == typeof(Type))
{
sort = string.Compare((string)_sortProperty.GetValue(x).ToString(),
(string)_sortProperty.GetValue(y).ToString());
}
else if (propType == typeof(bool))
{
sort = string.Compare((string)_sortProperty.GetValue(x).ToString(),
(string)_sortProperty.GetValue(y).ToString());
}
else
{
throw new NotSupportedException();
}
if (_sortDescription == ListSortDirection.Ascending)
{
sort = -sort;
}
return sort;
}
}
}