自定义集合具有排序功能。像datatable
2009-08-04 13:35
330 查看
当大家自定义对象时,无论你返回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;
}
}
}
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;
}
}
}
相关文章推荐
- 具有自定义分页和排序功能的DataGrid控件
- 点击DataGridView列标题排序后,绑定的DataTable也相应的排序, DataGridView和DataTable数据具有一致性 (新增加了sorted事件的排序方式)
- 自定义具有自动完成功能的ComboBox
- C++ STL 有关于SET集合部分 的自定义数据类型的排序 以及 pair的使用
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- 现集合类Sort自定义排序
- 集合框架_ArrayList存储自定义对象并排序案例
- Java编程之TreeSet排序两种解决方法(1)元素自身具备比较功能,元素需要实现Comparable接口覆盖compare(2)创建根据自定义Person类的name进行排序的Comparator
- Java基础知识强化之集合框架笔记46:Set集合之TreeSet存储自定义对象并遍历练习2(自然排序)
- [c#基础]泛型集合的自定义类型排序
- 一步步教你在SharePoint站点创建具有分页、排序、筛选功能的SPGridView Web部件
- day8 集合类的层次结构 jUnit 集合的高级功能---自动排序
- Java 实现Map集合排序功能
- 使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
- .NET/C#中对自定义对象集合进行自定义排序的方法
- 用泛型List实现集合类及排序功能
- 控件也是类——分步实现具有分页功能的自定义DataList控件【附源代码】
- 一个带自定义分页,排序功能的DATAGRID控件(公开源码)
- 自定义开发具有Ajax功能的JSF组件
- List集合排序(默认及自定义排序)