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;
}
else
{
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
{
get { return true; }
}

protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
List<T> items = this.Items as List<T>;

if (items != null)
{
ObjectPRopertyCompare<T> pc = new ObjectPRopertyCompare<T>(property, direction);
items.Sort(pc);
isSorted = true;
}
else
{
isSorted = false;
}

sortProperty = property;
sortDirection = direction;

this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}

protected override void RemoveSortCore()
{
isSorted = false;
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
//排序
public void Sort(PropertyDescriptor property, ListSortDirection direction)
{
this.ApplySortCore(property, direction);
}
}

你可能感兴趣的:(datagridview)