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);
}
}
list集合绑定在datagridview上时如何实现排序的更多相关文章
- jQuery1.8以上,ajaxSend,ajaxStart等一系列事件要绑定在document上才有效果
jQuery1.8以上,ajaxSend,ajaxStart等一系列事件要绑定在document上才有效果
- 为什么tap事件绑定在document上,而不是对象本身上
1.在移动端前端开发,click事件有300ms的延时,为了提升用户体验,快速响应.zepto添加了tap事件.tap是在手指触屏横纵向移动距离小于30px,触发tap事件.移动距离的判断是通过tou ...
- D3.js学习笔记(二)——使用绑定在DOM上的数据
简单例子 在这个例子中,你将会使用D3.js来将数据绑定到DOM元素上.然后再使用D3.js利用绑定到DOM元素上的数据来更新网页. 在上一章中,我们以下面这个页面作为开始的: <!DOCTYP ...
- 电脑出现“损坏的图像”窗口提示dll没有被指定在Windows上运行如何解决
电脑中出现了无法运行应用程序的情况,弹出一个“***.exe - 损坏的图像”的窗口,上面提示“***.dll没有被指定在Windows上运行……”,如果我们遇到这样的问题,应该要如何解决呢? 1.我 ...
- 解决IIS7.0服务和用户上传的文件分别部署在不同的电脑上时,解决权限的问题
为解决IIS服务和用户上传的文件分别部署在不同的电脑上时,解决权限的问题. 定义: A:iis服务器 B:文件服务器 步骤: 1.在B上创建一个用户[uploaduser](并设置密码) 2.给B上的 ...
- Android权限安全(12)apk安装在sd卡上时,如何保证数据安全
apk安装在sd卡上时,如果把sd卡拿下安在另一个手机B上,那么apk的数据就可以被B里的恶意应用访问了. 下面是android解决这个问题的方案: 绑定设备 1,绑定perDevice使得应用以及应 ...
- VC/MFC 当鼠标移到控件上时显示提示信息
VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...
- echarts柱状图修改背景线为网格线、去掉刻度标签、鼠标悬停在柱条上时变色、柱条圆角弧度、
option = { color: ['red'],//修改柱条颜色 tooltip : { triggerOn:'mousemove' }, grid: { left: '3%', right: ' ...
- 【转】使用Eclipse,将鼠标放在相应方法或字段等元素上时,无法显示提示
使用Eclipse编写java代码时,将鼠标放在相应方法或字段等元素上时,会有对应的说明或提示. 不过,常出现下面的问题: Note:An exception occurred while getti ...
随机推荐
- 坦克大战-C语言-详注版
代码地址如下:http://www.demodashi.com/demo/14259.html 坦克大战-C语言-详注版 概述 本文详述了C语言版坦克大战游戏的原理以及实现方法,对游戏代码进行了详细的 ...
- Timer与AlarmManager的差别
线程 通过调用Thread类的 start()方法来启动一个线程,这时此线程处于就绪(可执行)状态.但此时并没有执行,它须要CPU时间片. 一旦得到CPU时间片.就会执行run()方法. run()的 ...
- 一般web典型的项目目录结构
本文转自:http://blog.sina.com.cn/s/blog_4758a28b0100l3lp.html WebRoot- -common (系统框架公用jsp 如foote ...
- HDFS配额管理指南
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_quota_admin_guide.html Hadoop分布式文件系统(HDFS)允许管理员为每个 ...
- read/write函数与(非)阻塞I/O的概念
一.read/write 函数 read函数从打开的设备或文件中读取数据. #include <unistd.h> ssize_t read(int fd, void *buf, size ...
- Linux中断 - High level irq event handler
一.前言 当外设触发一次中断后,一个大概的处理过程是: 1.具体CPU architecture相关的模块会进行现场保护,然后调用machine driver对应的中断处理handler 2.mach ...
- Python2 字典 has_key() 方法
描述 Python2 字典 has_key() 方法用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 官方文档推荐用 in 操作符,因为它更短更通俗易懂.h ...
- Python2 元组 cmp() 方法
描述 Python2 元组 cmp() 方法用于比较两个元组,如果 T1< T2返回 -1, 如果 T1== T2返回 0, 如果 T1> T2返回 1. 语法 cmp() 方法语法: c ...
- Action中如何通过@Autowired自动注入spring bean ?
1.讲Action纳入spring的IOC控制 <!-- 采用注解方式自动扫描装配 --> <context:component-scan base-package="co ...
- 修改 Input placeholder 的样式
::-webkit-input-placeholder { /* WebKit browsers */ color: #ccc; } :-moz-placeholder { /* Mozilla Fi ...