博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
list集合绑定在datagridview上时如何实现排序
阅读量:5896 次
发布时间:2019-06-19

本文共 3152 字,大约阅读时间需要 10 分钟。

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

转载地址:http://khxsx.baihongyu.com/

你可能感兴趣的文章
Struts秘籍之起式:第1.3式:迁移至Struts 1.1
查看>>
绿色PLSQL/Developer搭配Oracle精简客户端使用
查看>>
ViewPager Banner(广告墙)
查看>>
Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)(Greenwich.RELEASE)
查看>>
iOS开发20:Navigation Bar的简单设置
查看>>
iOS开发24:使用SQLite3存储和读取数据
查看>>
GMF树形布局 2 实现展开/折叠
查看>>
Cocos2dx 2.0x Touch事件
查看>>
php判断是否登录
查看>>
Yii2 Unable to verify your data submission 错误-CSRF
查看>>
angularjs-paste-upload
查看>>
解除 Linux 系统的最大进程数和最大文件打开数限制
查看>>
Java类的修饰符判断:java.lang.reflect.Modifier
查看>>
使用优盘或者移动硬盘安装Ubuntu
查看>>
electron-创建一个hello world应用
查看>>
RXjs相关
查看>>
百练2973: Skew binary 数 之 Java 题解
查看>>
SaltStack配置管理
查看>>
linux基础命令 head
查看>>
在模板中将php数组转换成js对象
查看>>