winform datagridview 绑定泛型集合变得不支持排序的解决方案

时间:2023-03-09 19:31:19
winform datagridview 绑定泛型集合变得不支持排序的解决方案

原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案

案例:

  环境:Winform程序

  控件:Datagridview

  现象:Datagridview控件绑定到List<T>泛型数据上不支持排序

     Datagridview控件绑定到DataTable上可以支持排序

  结论:泛型会使Datagridview失去排序特性

  解决:实现BindingList<T>接口

  实现代码:

  

winform datagridview 绑定泛型集合变得不支持排序的解决方案
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text; namespace HOET.Plugins.Orders.Model
{
/// <summary>
/// 泛型会失去DataTable特性,DataGridView绑定List<T>后不支持排序
/// </summary>
/// <typeparam name="T"></typeparam>
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 = ; i < this.Count; i++)
{
if(Equals(prop.GetValue(this[i]),key))
{
return i;
}
} return -;
} 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 = ;
if(SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if(ret == && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[], 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 ? : -;
}
else if(o2 == null)
{
return ;
}
else if(type == typeof(char))
{
return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
else if (type.IsEnum ||type.IsPrimitive)
{
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());
}
}
}
}
winform datagridview 绑定泛型集合变得不支持排序的解决方案