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. eclipse cdt Program "make" not found in PATH

    eclipse cdt插件,开发c/c++程序,编译时报错Program "make" not found in PATH经查C:\MinGW\bin下确实无make.exe,有m ...

  2. tomcat部署war包访问显示404

    在eclipse导出的maven项目war包,放到tomcat下启动,顺利启动且无报错,但是浏览器访问该项目时显示404 原因:未在server.xml中配置这几个项目,导致服务器查找不到 解决方法: ...

  3. C-I/O操作函数详解

    EOF: End Of File, 文字流结尾, 这里的文字流可以是文件(file), 也可以是标准输入(stdin), 它的值在任何可能出现的字符之外(-1) 先列出三种基本类型操作函数 这里面返回 ...

  4. 使用ajax和window.history.pushState无刷新改变页面内容和地址栏URL (转)

    在访问现在很火的google plus时,细心的用户也许会发现页面之间的点击是通过ajax异步请求的,同时页面的URL发生了了改变.并且能够很好的支持浏览器的前进和后退.不禁让人想问,是什么有这么强大 ...

  5. iOS CPU占有率达到了100%甚至更多,然后导致App闪退

    今天在真机调试的过程中,发现了一个严重的问题,发现CPU的使用率竟然达到了100%,以至于会导致运行内存占用过高,被系统的看门狗机制给杀掉. 下面就讲一讲怎么去定位这个问题: 1.打开Xcode,把项 ...

  6. scikit-learn 入门练习

    1. 一个简单的SVM实例: from sklearn import svm X = [[2, 0], [1, 1], [2,3]] y = [0, 0, 1] clf = svm.SVC(kerne ...

  7. 在普通Java类里使用spring里注入的service、dao等

    版权声明:本文为博主武伟峰原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu. 在spring管理的web项目里,譬如Struts和spring的项目,配 ...

  8. Linux下安装配置Redis

    一 下载并安装 (1)下载: [root@localhost src]# wget http://download.redis.io/releases/redis-3.2.5.tar.gz (2)安装 ...

  9. Azure 怎么开通FTP

    1. 需要在Azure Portal设置端口:21, 1035, 1036, 1037,1038,1039,1040 2. 需要内网放开端口:21, 1035, 1036, 1037,1038,103 ...

  10. Logstash怎么导入csv

    Logstash.Conf input { file { type => "SSRCode" path => "E:/FTPRootWorkSpace/SD/ ...