自定义集合具有排序功能。像datatable

当大家自定义对象时,无论你返回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;

 

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值