list集合绑定在datagridview上时如何实现排序
2020-11-16 21:58
标签:des datagridview class code tar ext get int string set cti List DataGridViewTextBoxColumn GridView01NO=new
DataGridViewTextBoxColumn(); GridView01NO.HeaderText = "编号"; GridView01Name.HeaderText = "姓名"; dgv1.Columns.AddRange(new DataGridViewColumn[]
{GridView01NO,GridView01Name, }); datagridview的数据源是list集合时,是不能排序的,不过调用了BindingCollection类后就可以了, public class ObjectPRopertyCompare public ObjectPRopertyCompare(PropertyDescriptor property,
ListSortDirection direction) #region IComparer /// int returnValue; if (xValue is IComparable) if (direction == ListSortDirection.Ascending) public bool Equals(T xWord, T yWord) public int GetHashCode(T obj) #endregion protected override bool IsSortedCore protected override bool SupportsSortingCore protected override ListSortDirection SortDirectionCore protected override PropertyDescriptor SortPropertyCore protected override bool SupportsSearchingCore protected override void ApplySortCore(PropertyDescriptor property,
ListSortDirection direction) if (items != null) sortProperty = property; this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset,
-1)); protected override void RemoveSortCore() list集合绑定在datagridview上时如何实现排序,搜素材,soscw.com list集合绑定在datagridview上时如何实现排序 标签:des datagridview class code tar ext get int string set cti 原文地址:http://www.cnblogs.com/ChineseMoonGod/p/3700660.html
lst.Add(new Person("A", "1"));
lst.Add(new Person("C", "2"));
lst.Add(new Person("B", "3"));
BindingCollection
//加载数据
foreach (Person item
in lst)
{
objList.Add(item);
}
DataGridViewTextBoxColumn
GridView01Name=new DataGridViewTextBoxColumn();
GridView01NO.Name =
"strNo";
GridView01NO.DataPropertyName = "strNo";
GridView01Name.Name =
"strName";
GridView01Name.DataPropertyName = "strName";
//dgv1.DataSource = objList;
dgv1.DataSource = lst;
{
private
PropertyDescriptor property;
private ListSortDirection direction;
{
this.property =
property;
this.direction = direction;
}
/// 比较方法
///
/// 相对属性x
/// 相对属性y
///
public int Compare(T x, T y)
{
object xValue =
x.GetType().GetProperty(property.Name).GetValue(x, null);
object
yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);
{
returnValue = ((IComparable)xValue).CompareTo(yValue);
}
else if (xValue.Equals(yValue))
{
returnValue = 0;
}
else
{
returnValue = xValue.ToString().CompareTo(yValue.ToString());
}
{
return returnValue;
}
else
{
return returnValue * -1;
}
}
{
return xWord.Equals(yWord);
}
{
return
obj.GetHashCode();
}
}
public class BindingCollection
{
private bool isSorted;
private PropertyDescriptor
sortProperty;
private ListSortDirection sortDirection;
{
get {
return isSorted; }
}
{
get { return true; }
}
{
get { return sortDirection; }
}
{
get { return sortProperty; }
}
{
get { return true; }
}
{
List
{
ObjectPRopertyCompare
items.Sort(pc);
isSorted =
true;
}
else
{
isSorted = false;
}
sortDirection =
direction;
}
{
isSorted = false;
this.OnListChanged(new
ListChangedEventArgs(ListChangedType.Reset, -1));
}
//排序
public void Sort(PropertyDescriptor property, ListSortDirection
direction)
{
this.ApplySortCore(property,
direction);
}
}
上一篇:[PHP系列3]邮件发送相关
文章标题:list集合绑定在datagridview上时如何实现排序
文章链接:http://soscw.com/essay/21697.html