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

list集合绑定在datagridview上时如何实现排序的更多相关文章

  1. jQuery1.8以上,ajaxSend,ajaxStart等一系列事件要绑定在document上才有效果

    jQuery1.8以上,ajaxSend,ajaxStart等一系列事件要绑定在document上才有效果

  2. 为什么tap事件绑定在document上,而不是对象本身上

    1.在移动端前端开发,click事件有300ms的延时,为了提升用户体验,快速响应.zepto添加了tap事件.tap是在手指触屏横纵向移动距离小于30px,触发tap事件.移动距离的判断是通过tou ...

  3. D3.js学习笔记(二)——使用绑定在DOM上的数据

    简单例子 在这个例子中,你将会使用D3.js来将数据绑定到DOM元素上.然后再使用D3.js利用绑定到DOM元素上的数据来更新网页. 在上一章中,我们以下面这个页面作为开始的: <!DOCTYP ...

  4. 电脑出现“损坏的图像”窗口提示dll没有被指定在Windows上运行如何解决

    电脑中出现了无法运行应用程序的情况,弹出一个“***.exe - 损坏的图像”的窗口,上面提示“***.dll没有被指定在Windows上运行……”,如果我们遇到这样的问题,应该要如何解决呢? 1.我 ...

  5. 解决IIS7.0服务和用户上传的文件分别部署在不同的电脑上时,解决权限的问题

    为解决IIS服务和用户上传的文件分别部署在不同的电脑上时,解决权限的问题. 定义: A:iis服务器 B:文件服务器 步骤: 1.在B上创建一个用户[uploaduser](并设置密码) 2.给B上的 ...

  6. Android权限安全(12)apk安装在sd卡上时,如何保证数据安全

    apk安装在sd卡上时,如果把sd卡拿下安在另一个手机B上,那么apk的数据就可以被B里的恶意应用访问了. 下面是android解决这个问题的方案: 绑定设备 1,绑定perDevice使得应用以及应 ...

  7. VC/MFC 当鼠标移到控件上时显示提示信息

    VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...

  8. echarts柱状图修改背景线为网格线、去掉刻度标签、鼠标悬停在柱条上时变色、柱条圆角弧度、

    option = { color: ['red'],//修改柱条颜色 tooltip : { triggerOn:'mousemove' }, grid: { left: '3%', right: ' ...

  9. 【转】使用Eclipse,将鼠标放在相应方法或字段等元素上时,无法显示提示

    使用Eclipse编写java代码时,将鼠标放在相应方法或字段等元素上时,会有对应的说明或提示. 不过,常出现下面的问题: Note:An exception occurred while getti ...

随机推荐

  1. ocat

    <!DOCTYPE html> <html lang="zh-CN" > <head><meta http-equiv="Con ...

  2. 自己总结的javascript基础知识

    为了面试自己写的概要,参考文献:<javascript高级程序设计> 1.选择符API *querySelector() querySelector()接收一个css选择符,返回与选择符匹 ...

  3. PHP实现4种排序算法

    1.冒泡排序 冒泡排序其实是基于“交换”.每次从第一个记录开始,一.二两个记录比较,大的往后放,二三两个记录比较...依次类推,这就是一趟冒泡排序.每一趟冒泡排序后,无序序列中值最大的记录冒到序列末尾 ...

  4. 将数据库dbcp连接池改为c3p0连接池(草稿,别点)

    D:\Develop\apache-tomcat-7.0.52\bin\catalina.bat run[2017-12-26 02:31:29,174] Artifact pers:war expl ...

  5. c语言格式大整理

    1.C语言中,非零值为真,真用1表示:零值为假,假用0表示. 2.转义字符参考: \a 蜂鸣,响铃 \b 回退:向后退一格 \f 换页 \n 换行 \r 回车,光标到本行行首 \t 水平制表 \v 垂 ...

  6. gVim 中文内容显示为乱码的解决办法

    http://blog.csdn.net/sunmanzth/article/details/6143600打开vimrc文件,在vim的安装目录下可以找到该文件,或在windows下是在vim/gv ...

  7. Google Map 形状显示

    添加多段线 Polyline 构造函数带有一组用于指定线的 LatLng 坐标的 PolylineOptions,以及一组用于调整多段线视觉行为的样式. Polyline 对象在地图上绘制为一系列直线 ...

  8. Sourcetree 更新git账号密码

    删除Sourcetree 缓存文件(只需要删密码文件),文件位置: Mac: ~/Library/Application Support/SourceTree Windows: C:\Users\US ...

  9. Red hat linux ping: unknown host www.baidu.com

    "ping: unknown host www.baidu.com" 解决方案: 如果某台Linux服务器ping不通域名, 如下提示: [root@localhost ~]# p ...

  10. selenium python学习笔记---添加等待时间

    http://selenium-python.readthedocs.io/waits.html 有时候为了保证脚步运行的稳定性,需要在脚本中添加等待时间 添加休眠:需要引入time包,选择一个固定的 ...