1. public class ObjectPropertyCompare<T> : IComparer<T>
  2. {
  3. private readonly PropertyDescriptor property;
  4. private readonly ListSortDirection direction;
  5.  
  6. public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
  7. {
  8. this.property = property;
  9. this.direction = direction;
  10. }
  11.  
  12. public int Compare(T x, T y)
  13. {
  14. object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
  15. object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);
  16.  
  17. int returnValue;
  18.  
  19. if (xValue is IComparable)
  20. {
  21. returnValue = ((IComparable)xValue).CompareTo(yValue);
  22. }
  23. else if (xValue.Equals(yValue))
  24. {
  25. returnValue = ;
  26. }
  27. else
  28. {
  29. returnValue = xValue.ToString().CompareTo(yValue.ToString());
  30. }
  31.  
  32. if (direction == ListSortDirection.Ascending)
  33. {
  34. return returnValue;
  35. }
  36. return returnValue * -;
  37. }
  38. }
  1. public class BindingCollection<T> : BindingList<T>
  2. {
  3. private bool isSorted;
  4. private PropertyDescriptor sortProperty;
  5. private ListSortDirection sortDirection;
  6.  
  7. public BindingCollection()
  8. {
  9. }
  10.  
  11. public BindingCollection(IList<T> list)
  12. : base(list)
  13. {
  14. }
  15.  
  16. protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
  17. {
  18. var items = Items as List<T>;
  19.  
  20. if (items != null)
  21. {
  22. var pc = new ObjectPropertyCompare<T>(property, direction);
  23. try
  24. {
  25. items.Sort(pc);
  26. isSorted = true;
  27. }
  28. catch (Exception)
  29. {
  30. isSorted = false;
  31. }
  32. }
  33. else
  34. {
  35. isSorted = false;
  36. }
  37.  
  38. sortProperty = property;
  39. sortDirection = direction;
  40.  
  41. OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -));
  42. }
  43.  
  44. protected override bool IsSortedCore
  45. {
  46. get { return isSorted; }
  47. }
  48.  
  49. protected override bool SupportsSortingCore
  50. {
  51. get { return true; }
  52. }
  53.  
  54. protected override ListSortDirection SortDirectionCore
  55. {
  56. get { return sortDirection; }
  57. }
  58.  
  59. protected override PropertyDescriptor SortPropertyCore
  60. {
  61. get { return sortProperty; }
  62. }
  63.  
  64. protected override void RemoveSortCore()
  65. {
  66. isSorted = false;
  67. OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -));
  68. }
  69. }

Winform DataGridView扩展的更多相关文章

  1. C#实现WinForm DataGridView控件支持叠加数据绑定

    我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...

  2. 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

    上周在开发Winform 项目中,我曾遇到一个看似简单,但一直都没有解决的问题,那就是:设置winform DataGridView控件的行DefaultCellStyle,但却没有任何变化,我也曾求 ...

  3. C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色

    在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别.为了更好地区分不同类型的日志,我们需要使 ...

  4. WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  5. C# winform DataGridView 常见属性

    C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...

  6. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

  7. [WinForm]DataGridView列头右键菜单

    [WinForm]DataGridView列头右键菜单 前言 继续"不误正业" - - #,记录一下.有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列, ...

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

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

  9. [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

随机推荐

  1. Cassandra 计数器counter类型和它的限制

    文档基础 Cassandra 2.* CQL3.1 翻译多数来自这个文档 更新于2015年9月7日,最后有参考资料 作为Cassandra的一种类型之一,Counter类型算是限制最多的一个.Coun ...

  2. phpStorm使用技巧及快捷键

    下面是PhpStorm的注册码.Key,其license由用户名和License值组成. User name: EMBRACE License key: ===== LICENSE BEGIN === ...

  3. Ghost命令使用方法

    我们知道,一般使用Ghost时,都是在DOS提示符后先键入"Ghost",然后再进入Ghost的图形界面操作:那么可不可以让Ghost也只通过命令行的方式工作呢?答案是肯定的,在键 ...

  4. linux回环网卡驱动设计

    回环网卡驱动 1.回环网卡和普通网卡的区别是他是虚拟的不是实际的物理网卡,它相当于把普通网卡的发送端和接收端短接在一起. 2.在内核源代码里的回环网卡程序(drivers/net/loopback.c ...

  5. 在EF的code frist下写稳健的权限管理系统:开篇(一)

    环境:EF6.0.0.0+Autofac3.5.0.0+MVC4.0+pure6.0+Jquery IDE:vs2012,数据库:vs2008r2 搭建环境如下: 我给它取名字为cactus:仙人球, ...

  6. Jquery + echarts 使用

    常规用法,就不细说了,按照官网一步步来. 本文主要解决问题(已参考网上其他文章): 1.把echarts给扩展到JQuery上,做到更方便调用. 2.多图共存 3.常见的X轴格式化,钻取时传业务实体I ...

  7. wpf 获取DataGrid某一个单元格,设置此单元格ToolTip内容和背景颜色

    public void GetCell()        {            for (int i = 0; i < this.datagrid1.Items.Count; i++)    ...

  8. XAML(3) - 附带属性

    WPF元素也可以从父元素中获得特性.例如,如果Button元素为了Canvas元素中,按钮的Top和Lef属性把父元素的名称作为前缀.这种属性成为附带属性: <Canvas> <Bu ...

  9. MongoDB分片简单实例

    分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据也足以提供可接受的读写吞吐量.这时,我 ...

  10. mysql索引合并:一条sql可以使用多个索引

    前言 mysql的索引合并并不是什么新特性.早在mysql5.0版本就已经实现.之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念.本文会通过一些示例来说明如何使用索 ...