public class ObjectPropertyCompare<T> : IComparer<T>
{
private readonly PropertyDescriptor property;
private readonly ListSortDirection direction; public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
{
this.property = property;
this.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 is IComparable)
{
returnValue = ((IComparable)xValue).CompareTo(yValue);
}
else if (xValue.Equals(yValue))
{
returnValue = ;
}
else
{
returnValue = xValue.ToString().CompareTo(yValue.ToString());
} if (direction == ListSortDirection.Ascending)
{
return returnValue;
}
return returnValue * -;
}
}
public class BindingCollection<T> : BindingList<T>
{
private bool isSorted;
private PropertyDescriptor sortProperty;
private ListSortDirection sortDirection; public BindingCollection()
{
} public BindingCollection(IList<T> list)
: base(list)
{
} protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
var items = Items as List<T>; if (items != null)
{
var pc = new ObjectPropertyCompare<T>(property, direction);
try
{
items.Sort(pc);
isSorted = true;
}
catch (Exception)
{
isSorted = false;
}
}
else
{
isSorted = false;
} sortProperty = property;
sortDirection = direction; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -));
} 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 void RemoveSortCore()
{
isSorted = false;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -));
}
}

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. 线程间通信--wait和notify

    使用wait.notify方法实现线程间的通信(注意这两个方法都是object的类的方法,换句话说java为所有的对象都提供了这两个方法) 1.wait和notify必须配合synchronized关 ...

  2. Centos 7配置ntp时间同步

    1.NTP时钟同步方式说明     NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步: 1)直接同步      使用ntpdate命令进行同步,直接进行时间变更.如果服务器上存在一个1 ...

  3. sprintf函数减少字符串拼接错误

    $return_string=""; foreach($cat_list as $value){ $return_string .= sprintf('<dd>< ...

  4. EcShop二次开发系列教程–总纲

    EcShop作为老牌的B2C独立网店系统,功能非常全名,强大的文件.数据库缓存机制,保证前后台系统执行速度更快.系统平稳运行.但是过多的功能也或多或少的会影响到系统的整个效率,所有在使用EcShop搭 ...

  5. MarkDown写作技巧

    前言 年轻的我们往往苦恼于没有充实的社会经历.社会阅历,很难有较强的个人能力.个人魅力!就个人能力而言,本 人主要把它分为两种:“吸收能力”和“输出能力”.吸收能力主要体现了个人对外界知识的摄入能力, ...

  6. linux之i2c子系统架构---总线驱动

    编写i2c设备驱动(从设备)一般有两种方式: 1.用户自己编写独立的从设备驱动,应用程序直接使用即可. 2.linux内核内部已经实现了一个通用的设备驱动,利用通用设备驱动编写一个应用程序(用户态驱动 ...

  7. About Inside the Azure Storage Outage of November 18th

    Channel 9的官方解释 http://channel9.msdn.com/posts/Inside-the-Azure-Storage-Outage-of-November-18th 曾经在自己 ...

  8. oracle-审计导数

    1.因审计需求,需要将MySQL.Oracle数据库中需要的表数据导入到SqlSERVER进行审计. 2.之前的方法:   A. oracle组将表dump下来,进行压缩,传送到oracle导数服务器 ...

  9. Spring Dynamic Modules - DMserver

    spring dm server 官网:http://static.springsource.com/projects/dm-server/1.0.x/programmer-guide/htmlsin ...

  10. [转]Squid中的日志出现TCP_CLIENT_REFRESH_MISS的问题排除

    转自:http://www.php-oa.com/2008/07/15/tcp_client_refresh_miss.html 今天检查Squid发现大量的日志出现TCP_CLIENT_REFRES ...