天天看點

list集合綁定在datagridview上時如何實作排序

List<Person> lst = new List<Person>();

lst.Add(new Person("A", "1"));

lst.Add(new Person("C", "2"));

lst.Add(new Person("B", "3"));

BindingCollection<Person> objList = new

BindingCollection<Person>();

//加載資料

foreach (Person item

in lst)

{

objList.Add(item);

}

DataGridViewTextBoxColumn GridView01NO=new

DataGridViewTextBoxColumn();

DataGridViewTextBoxColumn

GridView01Name=new DataGridViewTextBoxColumn();

GridView01NO.HeaderText = "編号";

GridView01NO.Name =

"strNo";

GridView01NO.DataPropertyName = "strNo";

GridView01Name.HeaderText = "姓名";

GridView01Name.Name =

"strName";

GridView01Name.DataPropertyName = "strName";

dgv1.Columns.AddRange(new DataGridViewColumn[]

{GridView01NO,GridView01Name, });

//dgv1.DataSource = objList;

dgv1.DataSource = lst;

datagridview的資料源是list集合時,是不能排序的,不過調用了BindingCollection類後就可以了,

public class ObjectPRopertyCompare<T> :

System.Collections.Generic.IComparer<T>

private

PropertyDescriptor property;

private ListSortDirection direction;

public ObjectPRopertyCompare(PropertyDescriptor property,

ListSortDirection direction)

this.property =

property;

this.direction = direction;

#region IComparer<T>

/// <summary>

/// 比較方法

/// </summary>

/// <param name="x">相對屬性x</param>

/// <param

name="y">相對屬性y</param>

///

<returns></returns>

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);

int returnValue;

if (xValue is IComparable)

returnValue = ((IComparable)xValue).CompareTo(yValue);

else if (xValue.Equals(yValue))

returnValue = 0;

else

returnValue = xValue.ToString().CompareTo(yValue.ToString());

if (direction == ListSortDirection.Ascending)

return returnValue;

return returnValue * -1;

public bool Equals(T xWord, T yWord)

return xWord.Equals(yWord);

public int GetHashCode(T obj)

return

obj.GetHashCode();

#endregion

public class BindingCollection<T> : BindingList<T>

private bool isSorted;

private PropertyDescriptor

sortProperty;

private ListSortDirection sortDirection;

protected override bool IsSortedCore

get {

return isSorted; }

protected override bool SupportsSortingCore

get { return true; }

protected override ListSortDirection SortDirectionCore

get { return sortDirection; }

protected override PropertyDescriptor SortPropertyCore

get { return sortProperty; }

protected override bool SupportsSearchingCore

protected override void ApplySortCore(PropertyDescriptor property,

List<T> items =

this.Items as List<T>;

if (items != null)

ObjectPRopertyCompare<T> pc = new ObjectPRopertyCompare<T>(property,

direction);

items.Sort(pc);

isSorted =

true;

isSorted = false;

sortProperty = property;

sortDirection =

direction;

this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset,

-1));

protected override void RemoveSortCore()

this.OnListChanged(new

ListChangedEventArgs(ListChangedType.Reset, -1));

//排序

public void Sort(PropertyDescriptor property, ListSortDirection

direction)

this.ApplySortCore(property,