本文章转载:http://yuyingying1986.blog.hexun.com/30905610_d.html

DataGridView绑定List<T>类时候,不会自动的进行排序。默认BinddingList<T> 不支持排序。

解决办法:一、手动实现DataGridView列标题的点击排序事件。二、自定义实现BinddingList<T>类 支持排序。

我们常常使用DataGridView 控件,这个控件在绑定数据源后,常常不能排序,正好我现在做的项目中也遇上了这个问题,所以上网查了一些资料,解决了这个问题,下面是我解决的方法

1.创健一个专门用来排序的类

处理手段 

  做排序处理,做本质的办法是继承ICompare接口,重新Compare方法。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; namespace ConcreteMonitor.VehicleManagement
{
class ObjectPropertyCompare<T> : System.Collections.Generic.IComparer<T> 
{ private PropertyDescriptor _property;
public PropertyDescriptor property
{
get { return _property; }
set { _property = value; }
} private ListSortDirection _direction;
public ListSortDirection direction
{
get { return _direction; }
set { _direction = value; }
} public ObjectPropertyCompare()
{ } public ObjectPropertyCompare(PropertyDescriptor prop, ListSortDirection direction)
{
_property = prop;
_direction = direction;
} 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 == null && yValue == null)
{
returnValue = 0;
}
else if (xValue == null)
{
returnValue = -1;
}
else if (yValue == null)
{
returnValue = 1;
}
else 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;
} }
}
} 2.创建用于绑定数据源的类 using System.Text;
using System.ComponentModel;
using JtxSignal;
using JtxSignal.Signals;
using ConcreteMonitor.Database;
using System.Data;
using System.Collections;
using ConcreteMonitor.Properties; namespace ConcreteMonitor.VehicleManagement
{
public class BindingCollection<T> : BindingList<T>
{
protected override bool SupportsSortingCore
{
get { return true; }
} protected override bool SupportsSearchingCore
{
get { return true; }
} private bool isSortedCore = true;
protected override bool IsSortedCore
{
get
{
return isSortedCore;
}
} private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
protected override ListSortDirection SortDirectionCore
{
get
{
return sortDirectionCore;
}
} private PropertyDescriptor sortPropertyCore = null;
protected override PropertyDescriptor SortPropertyCore
{
get
{
return sortPropertyCore;
}
} protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
List<T> items = this.Items as List<T>; if (items != null)
{
ObjectPropertyCompare<T> pc = new ObjectPropertyCompare<T>(prop, direction);
items.Sort(pc);
isSortedCore = true;
sortDirectionCore = direction;
sortPropertyCore = prop;
}
else
{
isSortedCore = false;
} this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
} protected override void RemoveSortCore()
{
isSortedCore = false;
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
} 3.使用

List<T> list=new List<T>();
DataGridView.DataSource = new BindingCollection<自己定义的类>(list); 4.完成

  

第二种写法:

  public class SortableBindingList<T> : BindingList<T>
{
private readonly Dictionary<Type, PropertyComparer<T>> comparers;
private bool isSorted;
private ListSortDirection listSortDirection;
private PropertyDescriptor propertyDescriptor; public SortableBindingList()
: base(new List<T>())
{
this.comparers = new Dictionary<Type, PropertyComparer<T>>();
} public SortableBindingList(IEnumerable<T> enumeration)
: base(new List<T>(enumeration))
{
this.comparers = new Dictionary<Type, PropertyComparer<T>>();
} protected override bool SupportsSortingCore
{
get { return true; }
} protected override bool IsSortedCore
{
get { return this.isSorted; }
} protected override PropertyDescriptor SortPropertyCore
{
get { return this.propertyDescriptor; }
} protected override ListSortDirection SortDirectionCore
{
get { return this.listSortDirection; }
} protected override bool SupportsSearchingCore
{
get { return true; }
} protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
List<T> itemsList = (List<T>)this.Items; Type propertyType = property.PropertyType;
PropertyComparer<T> comparer;
if (!this.comparers.TryGetValue(propertyType, out comparer))
{
comparer = new PropertyComparer<T>(property, direction);
this.comparers.Add(propertyType, comparer);
} comparer.SetPropertyAndDirection(property, direction);
itemsList.Sort(comparer); this.propertyDescriptor = property;
this.listSortDirection = direction;
this.isSorted = true; this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
} protected override void RemoveSortCore()
{
this.isSorted = false;
this.propertyDescriptor = base.SortPropertyCore;
this.listSortDirection = base.SortDirectionCore; this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
} protected override int FindCore(PropertyDescriptor property, object key)
{
int count = this.Count;
for (int i = 0; i < count; ++i)
{
T element = this[i];
if (property.GetValue(element).Equals(key))
{
return i;
}
} return -1;
}
} public class PropertyComparer<T> : IComparer<T>
{
private readonly IComparer comparer;
private PropertyDescriptor propertyDescriptor;
private int reverse; public PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
{
this.propertyDescriptor = property;
Type comparerForPropertyType = typeof(Comparer<>).MakeGenericType(property.PropertyType);
this.comparer = (IComparer)comparerForPropertyType.InvokeMember("Default", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public, null, null, null);
this.SetListSortDirection(direction);
} #region IComparer<T> Members public int Compare(T x, T y)
{
return this.reverse * this.comparer.Compare(this.propertyDescriptor.GetValue(x), this.propertyDescriptor.GetValue(y));
} #endregion private void SetPropertyDescriptor(PropertyDescriptor descriptor)
{
this.propertyDescriptor = descriptor;
} private void SetListSortDirection(ListSortDirection direction)
{
this.reverse = direction == ListSortDirection.Ascending ? 1 : -1;
} public void SetPropertyAndDirection(PropertyDescriptor descriptor, ListSortDirection direction)
{
this.SetPropertyDescriptor(descriptor);
this.SetListSortDirection(direction);
}
} 调用:   List<Model.EmailList> emails = (List<Model.EmailList>)e.Data;
 dgvEmails.DataSource =new SortableBindingList<Model.EmailList>(emails);

  

DataGridView绑定BindingList<T>带数据排序的类的更多相关文章

  1. C# 中 datagridview 绑定BindingList类型和更新

    C# 中的datagridview是一个非常有用且强大的控件,可以用来绑定数据库.绑定LIST类型的变量等等. 这里我们说一说绑定List类型并实时更新datagridview的情况.实时更新,指的是 ...

  2. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  3. DataGridView绑定数据、删除数据

    定义学生类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  4. winform datagridview 绑定泛型集合变得不支持排序的解决方案

    原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案 案例: 环境:Winform程序 控件:Datagridview 现象:Datagridview控件绑定到List ...

  5. DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样

    '近日有本论坛网友问:DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样 '今晚正好闲着没事,加之以前也没用到过这个需求,所以就写了个模拟功能,供各位坛友酌情参考. ...

  6. c# winform 中DataGridView绑定List<T> 不能显示数据

    遇到问题 DataGridView绑定List后,List更新后再次绑定不显示数据 datagridview 绑定数据源的时候 用List是不能显示修改内容的..要用binginglist<T& ...

  7. DataGridView绑定泛型List时,利用BindingList来实现增删查改

    DataGridView绑定泛型List时,利用BindingList来实现增删查改  一.   DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...

  8. DataGridView 绑定数据方法

    DataGridView控件用于显示来自多种外部数据源中的数据,用户可以在此控件添加行和列,并可以填充数据.   如要让DataGridView显示数据库中的数据,只需要将此控件绑定到挑用数据库的数据 ...

  9. DataGridView绑定数据源的几种方式

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

随机推荐

  1. Vijos1386 IOI2007 矿工配餐 动态规划

    感觉早些年IOI的题都不难啊,也就NOIp难度……现在貌似变难了 状态用dp[n][a1][b1][a2][b2]表示 n表示处理到前n个餐车 第一组矿工得到的最近一种食物用a1表示,a1的上一种食物 ...

  2. LAMP的编译日志,

    在CentOS5.2上,编译LAMP的,两年前测试通过的,现在留印 ### 在记事本中 ,不要打开 自动换行,否则一些命令 无法正常运行###把源文件考到/src/目录下,然后进入/src////// ...

  3. shell之sort

    转http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html) sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟 ...

  4. c++ undefined reference to mysqlinit

    Solved g++ $(mysql_config --cflags) file.cpp -o filename $(mysql_config --libs)

  5. apache-php安装mysql简单方法

    1.启用mysql功能,在php.ini中 extension=php_mysql.dll extension=php_mysqli.dll 2. 修改extension_dir = "ex ...

  6. PHP之验证码识别

    首先推荐几篇有关验证码识别的文章,觉得不错 php实现验证码的识别(初级篇) 关于bp神经网格识别验证码 一.思路 碰见一个验证码,如果我们想要识别它,我们需要的是做什么呢? 我们先观察几个验证码.. ...

  7. 插件和过滤器装饰器开发中的感悟-python-django

    写这篇随笔是因为今天自己在写插件和过滤方法的过程中碰壁了,折腾了好久终于稍微发现些问题,在此记下,以作备忘. 在看了xadmin的插件机制后,笔者也想使用该思想来扩展kadmin中视图的方法. 例如, ...

  8. Android 之夜间模式(多主题)的实现

    引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...

  9. 常用Firefox扩展

    最近思维混乱,无心做事,故整理下东西.(PS:有些是firefox自带的.) 1.标签页管理器 2.1.41 用途:在新标签页打开书签.历史.地址.搜索. 主页:http://www.firefox. ...

  10. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...