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. 平衡二叉树(AVL)

    性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树. 定义:平衡二叉树或为空树,或为如下性质的二叉排序树: (1)左右子树深度之差的绝对值不超过1; (2 ...

  2. mouseout以及mouseover

    两个图层 红色图层代表outside蓝色图层代表inside dom结构如下 <div id="outside">      <div id="insi ...

  3. linux下的循环命令写法

    直切正题 方法一:利用while do循环,举例,while true;do ls;sleep 1;done  解释,该命令为每秒执行ls查询命令,sleep 1 为每秒循环,其他命令可直接替换 ls ...

  4. 查看SDCard是否被挂载

    获取Environment.getExternalStorageState(),然后得到的字符串进行查看 //android.os.Environment.MEDIA_MOUNTED="mo ...

  5. VLC 重新编译第三方库的预编译包contrib

    VLC的引用了很多开源的第三方库,均放到VLC目录下的contrib中(本来开始编译是2.1.x以上版本,以前或以后可能会变化),在Windows版本编译中,contrib文件夹中仅仅下载VLC官网上 ...

  6. 【转】MessageBox

    MessageBox对话框是比较常用的一个信息对话框,其不仅能够定义显示的信息内容.信息提示图标,而且可以定义按钮组合及对话框的标题,是一个功能齐全的信息对话框. 1.函数原型及参数 function ...

  7. DELPHI 使用dbexpress控件连接MySQL数据库方法

    1. 在窗体上放置4个控件: SQLConnection1,SimpleDataSet1,DataSource1,DBGrid (注意: 如果 出现"Operation not allowe ...

  8. C#发送邮件源码

    介绍 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.SMTP协议属于TCP/IP协议 ...

  9. 机器学习简易入门(四)- logistic回归

    摘要:使用logistic回归来预测某个人的入学申请是否会被接受 声明:(本文的内容非原创,但经过本人翻译和总结而来,转载请注明出处) 本文内容来源:https://www.dataquest.io/ ...

  10. swift 2

    ... ..< func a(inout a)  //by ref  use &xx Parameters passed to functions are constants by de ...