DataGridView 绑定List 时 排序问题的解决
2008-10-24 13:41
483 查看
public class SortableBindingList <T> : BindingList <T>
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem;
public SortableBindingList() : base() { }
public SortableBindingList(IList <T> list) : base(list) { }
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override bool SupportsSearchingCore
{
get { return true; }
}
protected override bool IsSortedCore
{
get { return isSortedCore; }
}
protected override ListSortDirection SortDirectionCore
{
get { return sortDirectionCore; }
}
protected override PropertyDescriptor SortPropertyCore
{
get { return sortPropertyCore; }
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
for (int i = 0; i < this.Count; i++)
{
if (Equals(prop.GetValue(this[i]), key)) return i;
}
return -1;
}
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
isSortedCore = true;
sortPropertyCore = prop;
sortDirectionCore = direction;
Sort();
}
protected override void RemoveSortCore()
{
if (isSortedCore)
{
isSortedCore = false;
sortPropertyCore = null;
sortDirectionCore = ListSortDirection.Ascending;
Sort();
}
}
public string DefaultSortItem
{
get { return defaultSortItem; }
set
{
if (defaultSortItem != value)
{
defaultSortItem = value;
Sort();
}
}
}
private void Sort()
{
List <T> list = (this.Items as List <T> );
list.Sort(CompareCore);
ResetBindings();
}
private int CompareCore(T o1, T o2)
{
int ret = 0;
if (SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if (ret == 0 && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
if (property != null)
{
ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
}
}
if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
return ret;
}
private static int CompareValue(object o1, object o2, Type type)
{
//这里改成自己定义的比较
if(o1 == null)return o2==null ? 0 : -1;
else if(o2 == null) return 1;
else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
}
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem;
public SortableBindingList() : base() { }
public SortableBindingList(IList <T> list) : base(list) { }
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override bool SupportsSearchingCore
{
get { return true; }
}
protected override bool IsSortedCore
{
get { return isSortedCore; }
}
protected override ListSortDirection SortDirectionCore
{
get { return sortDirectionCore; }
}
protected override PropertyDescriptor SortPropertyCore
{
get { return sortPropertyCore; }
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
for (int i = 0; i < this.Count; i++)
{
if (Equals(prop.GetValue(this[i]), key)) return i;
}
return -1;
}
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
isSortedCore = true;
sortPropertyCore = prop;
sortDirectionCore = direction;
Sort();
}
protected override void RemoveSortCore()
{
if (isSortedCore)
{
isSortedCore = false;
sortPropertyCore = null;
sortDirectionCore = ListSortDirection.Ascending;
Sort();
}
}
public string DefaultSortItem
{
get { return defaultSortItem; }
set
{
if (defaultSortItem != value)
{
defaultSortItem = value;
Sort();
}
}
}
private void Sort()
{
List <T> list = (this.Items as List <T> );
list.Sort(CompareCore);
ResetBindings();
}
private int CompareCore(T o1, T o2)
{
int ret = 0;
if (SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if (ret == 0 && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
if (property != null)
{
ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
}
}
if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
return ret;
}
private static int CompareValue(object o1, object o2, Type type)
{
//这里改成自己定义的比较
if(o1 == null)return o2==null ? 0 : -1;
else if(o2 == null) return 1;
else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
}
相关文章推荐
- 解决DataGridView绑定List后不能排序的问题
- 解决DataGridView绑定List后不能排序的问题
- 解决DataGridView绑定List后不能排序的问题
- 获取DataGridView绑定BindingSource的数据表,并根据树控件点击进行筛选数据表,下拉控件自定义排序问题初步解决方法,解决了该行属于另一个表的错误提示.
- DataGridView绑定List<T>数据源时,点击DataGridView列头自动排序解决方法
- Winform中DataGridView绑定List<T>数据源时,点击DataGridView列头不能自动排序解决方法
- DataGridView未绑定列排序问题
- DataGridView绑定BindingList<T>带数据排序的类
- DataGridView中绑定List泛型的问题
- 关于List<Model>类型 GridView排序问题的解决
- List<T>绑定到datagridview时不能排序
- DataRow[]、List<DataRow>无法绑定到GridView的问题解决!
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
- datagridview 某行不跟随排序 问题解决
- 解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
- [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
- list集合绑定在datagridview上时如何实现排序
- 章鱼哥出品—VB.NET DataGridView绑定数据源 "与货币管理器的位置关联的行不能设置为不可见" 问题的解决
- list排序问题解决
- List<Object>集合按照Object的某个字段排序,解决取不到父类字段问题